I am trying to create a virtual environment using virtualenv
on Mac OS X El Capitan. I have installed Python 2.7.11 with brew
, which includes pip
, wheel
and setuptools
by default.
Hovewer, when I try to install virtualenv
following instructions in the documentation or from any other resource, I get several problems:
virtualenv
executable is not placed in /usr/local/bin
after pip
makes its job, so I need to ln -s
it by hand (it may indicate, that there is something wrong with installation on this step). virtualenv venv
, it creates new environment, catches Python 2.7.11 from brew
-installation, but: there is no pip
inside bin
folder. That means, that if I try which pip
, having venv
activated, it returns a global position of pip
— /usr/local/bin/pip
, not /path/to/venv/bin/pip
.As a consequence, installing packages inside venv
uses global pip
and installs them to a global sites-packages
, not that inside venv
, and it's quite the opposite of what environment should do.
Could you please suggest what may be wrong and how to fix it?
EDIT: The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.
With the default settings, venv will install both pip and setuptools. Using pip is the recommended way to install packages in Python, and setuptools is a dependency for pip . Because installing other packages is the most common use case for Python virtual environments, you'll want to have access to pip .
pip is a tool for installing packages from the Python Package Index. virtualenv is a tool for creating isolated Python environments containing their own copy of python , pip , and their own place to keep libraries installed from PyPI.
To create a virtual environment, go to your project's directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands. The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env .
Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
I'm putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!
I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip
from within the virtual environment installed to the global package location, and was not visible to the environment's python.
How I diagnosed this on my machine:
$ virtualenv env
$ source env/bin/activate
(env)$ which python
with output /Users/<username>/env/bin/python
(as expected)(env)$ which pip
with output /usr/local/bin/pip
(NOT expected)To check where our packages are going, we can try to install a package in the virtual environment:
(env)$ pip install HTTPServer
which succeeds(env)$ python -m HTTPServer
which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
(env)$ pip install HTTPServer
which produces Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
Double-checking, we see that there's no Pip in the environment's /bin folder:
$ ls env/bin activate activate.fish python python2 activate.csh activate_this.py python-config python2.7
And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.
Here's what I tried: - Reinstalling python brew uninstall python
followed by brew upgrade
and brew install python --build-from-source
- Installing pip using the get-pip.py command as described in the Pip documentation
Here's what I ruled out: - I was not using sudo pip ...
which caused similar problems in this other question and haven't done so at any time on this Python/pip install - My virtual environment didn't show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.
Ultimately, I found that eliminating the ~/.pydistutils.cfg
file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg
file were:
[global] verbose=1 [install] install-scripts=$HOME/bin [easy_install] install-scripts=$HOME/bin
Simply renaming the ~/.pydistutils.cfg
file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn't had any bad effects on my system, you may need to use the --user
flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer
). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With