Default libraries in freshly created Python virtual environment

随声附和 提交于 2020-12-31 23:51:44

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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

pip freeze | xargs pip uninstall -y

This will uninstall all those unwanted packages.



来源:https://stackoverflow.com/questions/27101695/default-libraries-in-freshly-created-python-virtual-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!