Pip default behavior conflicts with virtualenv?

Question:

I was following this tutorial

When I got to virtualenv flask command, I received this error message:

Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

This makes sense as the point of virtualenv is to create a new environment which you can control, and the --user command places everything in a specific location, defeating the objective of separation of dev environment.

It seems like pip defaults to --user installations though, can I change this default behavior? And, even better, can I get pip to play nice with virtualenv at all times?

To clarify, here is what my terminal looks like.

MELCHIOR:miguelgrinberg-microblog megablanc$ virtualenv flask
New python executable in flask/bin/python
Installing setuptools, pip, wheel...
  Complete output from command /Users/megablanc/Dev...log/flask/bin/python -c "import sys, pip; sys...d"] + sys.argv[1:]))" setuptools pip wheel:
  Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/Users/megablanc/Library/Python/2.7/bin/virtualenv", line 11, in <module>
    sys.exit(main())
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 832, in main
    symlink=options.symlink)
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 1004, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 969, in install_wheel
    'PIP_NO_INDEX': '1'
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 910, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /Users/megablanc/Dev...log/flask/bin/python -c "import sys, pip; sys...d"] + sys.argv[1:]))" setuptools pip wheel failed with error code 1
Asked By: Nils Guillermin

||

Answers:

You don’t need to set the --user flag. After you create your virtualenv (virtualenv flask), activate it: source flask/bin/activate. Your shell should look something like (flask) ~>.

Once your virtualenv is activated, you should be able to pip install packages without issue. For example, pip install numpy. They’ll be installed in: lib/python2.6/site-packages/ (for whatever version of Python you are using)

Answered By: benlaird

There is a pip.conf file in ~/.pip. There I changed the flag user=true to user=false using the command gedit pip.conf, after which I am able to create virtual environment successfully.

Answered By: srikanth sridhar

In my case I was doing a make test for the python disco mapreduce library.

So I modified the Makefile test section and removed the --user flag.

Answered By: jmunsch

In my case it was a custom python installation from anaconda was interfering with the system installation. Check which pip… the solution is to either remove or move the custom installation of python.

Answered By: Pratik Khadloya

In my case, there was a file in /etc/pip.conf setting the user=true secretly. So, every time I activated a virtualenv, that config still affected the virtualenv.

Removing that line worked for me.

Answered By: MewX

Some people suggest you to edit /etc/pip.conf, which (i) requires superuser privileges and (ii) may break your whole system if done wrong. So, it is better to keep it to only your user.

Open up$HOME/.pip/pip.conf with a text editor. If it does not exist (which is the case in my Manjaro machine), create it. Then add the lines below, save and close.

[global]
user=false

When you change a setting for your programs, prefer under $HOME for doing that if possible, which will (i) persist the setting among updates and reinstalling system (if you have separated your disk to / and /home partitions, of course) and (ii) will not possibly break further upgrades of the program, in this case, pip.

Answered By: Eray Erdin

what worked for me was to change the $VIRTUAL_ENV_DIRECTORY/pyvenv.cfg to include-system-site-packages = true seems hacky though.

Answered By: Fabian Bosler

when your virutalenv myenv is activated (source myenv/bin/activate), remove --user.

Answered By: Behzad Sezari

This worked for me, I only changed the $VIRTUAL_ENV_DIRECTORY/pyvenv.cfg to

include-system-site-packages = true

The default setting

include-system-site-packages = false

Note:

> python --version 
Python 3.8.3 r c 1
Answered By: idyllictech

Although the question was asked quite a pretty while ago, maybe another answer would be useful to someone anyway.

If the described issue happens only when trying to install packages to virtualenv which is outside your home directory, the problem might be that user account you logged in does not have permissions on the folder you are trying to install to.

Use chown to change / raise user’s permission on the folder where target virtual environment is placed, for example:

chown username /var/www/your-webproject-folder/ -r

or

sudo chown username /var/www/your-webproject-folder/ -r

if you have to do it with root.

Answered By: yourihdx

Check if your the PIP_USER environment variable is set.
That fixed the issue for me.
If you are using GitPod then this is a known issue.

Answered By: Kartik Soneji
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.