Python and pip misconfiguration leads to package installation errors

Question:

I’m facing an issue with Python and pip where packages are not being installed under the correct version of Python. I am using Python 3.9.6:

python3 -V
Python 3.9.6

The pip version

pip3 --version
pip 21.2.4 from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)

The paths for pip3 and python3 are:

whereis pip3
pip3: /usr/bin/pip3

whereis python3
python3: /usr/bin/python3

Despite this, when I try to install google-cloud-storage using pip3 install google-cloud-storage, the package seems to install under Python 3.8. I receive the following warning:

WARNING: Target directory /usr/local/lib/python3.8/site-packages/googleapis_common_protos-1.63.0.dist-info already exists. Specify --upgrade to force replacement.

I attempted to resolve this by creating a virtual environment with:

/Library/Developer/CommandLineTools/usr/bin/python3 -m venv venv
And installing the package within the virtual environment:

/Users/username/Desktop/project/venv/bin/python3 -m pip install --upgrade google-cloud-storage

However, this did not resolve the issue. When I run my script, I get the following error:

Traceback (most recent call last):
  File "/Users/username/Desktop/project/main.py", line 3, in <module>
    from google.cloud import storage
ModuleNotFoundError: No module named 'google'

Additionally, google-cloud-storage does not appear in the output of pip list or pip3 list.

I installed Python 3.9 using Homebrew. I have deleted all Python versions from /usr/local/lib/. I don’t have any other version of Python other than Python 3.9 currently installed.
Could there be an issue with how pip is linked to Python versions on my system? How can I ensure that pip3 installs packages under Python 3.9?

Any help would be appreciated!

Asked By: dand1

||

Answers:

If you’re using Visual Studio Code, you can change the Python interpreter using the option in the bottom toolbar. If the text editor isn’t recognizing the correct interpreter, try adding or removing the appropriate python in the path variable of your device to fix the issue.

Answered By: Burhan

For some reason, under the pip config, there was an installation setup applied to all Python projects on the systems that The target option overrides the default install location for Python packages
global.target='/usr/local/lib/python3.8/site-packages'

Run pip config unset global.target, and things get back to normal

Answered By: dand1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.