Search code examples
pythonpython-3.xpippython-venv

Pip installing packages to global site-packages when inside virtual environment


Let me preface this by stating that I have read pip installing in global site-packages instead of virtualenv and it did not solve my problem.

When creating a virtual environment using python -m venv venv (or any other name) and then activating said venv using source venv/bin/activate, running pip install [package] actually installs the package to the (user) global python site packages, located in ~/.local/lib/python3.7/site-packages/.

Interestingly, it always tries to install the packages and does not recognize that they are installed globally, meaning it is looking for packages initially in the correct location.

I am running Manjaro Linux, and running the latest updates.

I created this virtual environment in ~/Stuff/tests/venv. Running which pip and which python returns:

(venv) angelin@asgard:~/Stuff/tests/venv $ which pip
/home/angelin/Stuff/tests/venv/bin/pip
(venv) angelin@asgard:~/Stuff/tests/venv $ which python
/home/angelin/Stuff/tests/venv/bin/python

Checking said pip executable shows the correct shebang:

(venv) angelin@asgard:~/Stuff/tests/venv $ cat bin/pip
#!/home/angelin/Stuff/tests/venv/bin/python
# -*- coding: utf-8 -*-
...

My path, when inside the virtualenv, points to the virtual environment first, too:

(venv) angelin@asgard:~/Stuff/tests/venv $ echo $PATH
/home/angelin/Stuff/tests/venv/bin:/home/angelin/.local/bin:...

Manually running python -m pip or venv/bin/python venv/bin/pip produces the same results.
The PYTHONPATH variable is not set.
I have tried reinstalling python and pip using pacman -S python python-pip, but I believe that only reinstalls the global package (located in /usr/bin and I didn't want to mess with the .local install in case I broke something even more than it is.

Any advice is appreciated.


Solution

  • I have a temporary workaround in place.

    /etc/pip.conf contained:

    [global]
    user = true
    

    There was no local pip config file, this was causing it to always pass the --user option. I am not sure how to configure my local file correctly, but I created it and set user = false and will just manually pass the --user option outside of venvs.

    This doesn't feel like a proper fix, as it was working normally 2 days ago and the global file hasn't been changed in more than a month. Maybe an update broke something, who knows. If anyone else has a better solution, feel free to share.