Sunday, September 4, 2022

[SOLVED] no module name pip : after changing permission of python folder

Issue

here is what i did before i messed up everything :

i tried to install a package using pip3 , after a long time the download finished and suddenly the error about permission came up because i forgot to use sudo at first and because I didn't want to download the packages again and didn't know where is the pip cache folder is , I did a very stupid thing i changed the permission of the entire python folder in the /usr/bin/ to install package without sudo , after this i tried this :

pip3 install tensorflow
File "/usr/bin/pip3", line 7, in <module>
    from pip import main
ImportError: No module named 'pip'

i got these damn error , can anybody help me fix this ?

Edit : here is my sequence of the command i used :

1 - pip3 install tensorflow -- the error came up

2 - sudo find /usr/lib/python3.5/ -type d -exec chmod 766 {} \;

3 - sudo find /usr/lib/python3.5/ -type f -exec chmod 766 {} \;


Solution

First, and foremost, I consider your approach quite unwise. You have now changed the permissions of all files and directories for the owner, the group, and others.

In principle you just needed to ensure that pip3 (by extension, your user-account) would be able write files and directories in a directory owned by root (presumably /usr/lib/python3.5/site-packages). You could have accomplished this by:

sudo chmod o+w /usr/lib/python3.5/site-packages

Alternatively you could have changed the ownership of this folder. IMPORTANT: when doing this kind of thing, be sure to know what you are doing, and don't forget to change everything back as soon as possible. Things can be broken, and security issues can be created.

Now as to a solution to your problem. You have now given the directories the following permissions -rwxrw-rw- (6 = 4 (read) + 2 (write)). However for users, and programs executed on its behalf, to do anything to/from a directory they need the right to execute. For this you should have used 5 instead of 6 (5 = 4 (read) + 1 (execute)). To correct:

sudo find /usr/lib/python3.5/ -type d -exec chmod 755 {} \;

Then, I think that for Python to correctly load compiled libraries (shared-objects or .so files) they should also have these permissions. Judging from my own Python directory I would probably do:

sudo find /usr/lib/python3.5/ -type f -exec chmod 644 {} \;
sudo find /usr/lib/python3.5/ -type f -iname '*.so' -exec chmod 755 {} \;

to set everything back to it's original state.

P.S. I am no expert in pip, so I have no idea what the protocol is to avoid pip from re-downloading upon retrying a failed installation.



Answered By - Tom de Geus
Answer Checked By - Timothy Miller (WPSolving Admin)