Default libraries in freshly created Python virtual environment

2.2k views Asked by At

I created a new virtual environment using the python virtualenv tool.

virtualenv venv

I then activated the virtual environment

source venv/bin/activate

Then I did a pip freeze and this is what I got:

(venv)$ pip freeze
Flask==0.10.1
Werkzeug==0.9.6
itsdangerous==0.24
lxml==3.4.0
numpy==1.9.1
pdir==0.2.2
virtualenv==1.11.6
wsgiref==0.1.2

I am wondering how so many libraries got installed when I did not even install anything in the virtual environment explicitly.

UPDATE 1: When some of the answers suggested, I used virtualenv --no-site-packages as well in Step 1, to create a fresh venv and the same problem persisted. As if using the argument had no effect at all.

UPDATE 2: I was able to solve the problem and have posted my experience below. As pointed in the comments; here is a related question; link, that helped me solve the problem.

4

There are 4 answers

0
Pranjal Mittal On

I was able to solve the problem. In my ~/.bash_profile file; I had the following line which was creating a problem:

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

As Martin Lewis, pointed out in an answer to this related question, --no-site-packages will remove the standard site-packages directory from sys.path. But anything else that lives in the standard Python path will still remain.

P.S: I am still not sure why virtualenv works this way. Why include the packages that are there on PYTHONPATH. But at least knowing this solves my problem.

2
Daniel Roseman On

Those are the libraries that are already installed globally on your system.

Best practice is to use the --no-site-packages option when creating the virtualenv in order to avoid this exact issue.

0
Maroun On

See the documentation - The --system-site-packages Option:

If you build with virtualenv --system-site-packages ENV, your virtual environment will inherit packages from /usr/lib/python2.7/site-packages (or wherever your global site-packages directory is).

This can be used if you have control over the global site-packages directory, and you want to depend on the packages there. If you want isolation from the global system, do not use this flag.

These libraries are already installed in your system. Unless you specify --no-site-packages, they'll be inherited.

0
Hussain Pettiwala On

A 'hackish' solution will be to activate virtual environment and run

pip freeze | xargs pip uninstall -y

This will uninstall all those unwanted packages.