ChamaraL
ChamaraL

Reputation: 431

Messed up pip environment and unable to python import modules

My python related versions and paths are as follows

pip -V

output

pip 19.1.1 from /home/USER_NAME/.local/lib/python3.6/site-packages/pip (python 3.6)
which pip

output

/home/USER_NAME/.local/bin/pip
python -V

output

Python 3.6.7
which python

output

/home/USER_NAME/bin/python

The problem is I used several methods to update pip and everything messed up. So now I can use python modules even though they are installed using pip. Example

pip install requests 

says

Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.18.4)

but when I import requests in python code I get this

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests

The reason is that pip links to .local directory how to resolve this I tried uninstalling pip and reinstalling but didn't help. Help is appreciated

Upvotes: 1

Views: 609

Answers (1)

Vithulan
Vithulan

Reputation: 300

Even though which python shows /home/USER_NAME/bin/python because it is symlinked to the global scope python which is in /usr/bin/ But the pip is installed in the local scope of the user (/home/$USER/.local/bin/). So when you run

pip install requests It checks for /home/$USER/.local so the requirement satisfies.

The solution is, both pip and python should be in the same scope (either in global or the user's)

You can uninstall your local pip (pip uninstall pip) and use global pip and global scope python

OR

You can symlink /home/$USER/bin/python to local user scope python (ln -s /home/$USER/.local/bin/python3 /home/$user/bin/python) after removing the current symlink in $HOME/bin/python

Upvotes: 1

Related Questions