virtualenv –no-site-packages和pip仍在查找全局软件包吗?

问题:virtualenv –no-site-packages和pip仍在查找全局软件包吗?

我印象中virtualenv --no-site-packages会创建一个完全独立和隔离的Python环境,但事实并非如此。

例如,我在全局安装了python-django,但希望使用其他Django版本创建virtualenv。

$ virtualenv --no-site-packages foo       
New python executable in foo/bin/python
Installing setuptools............done.
$ pip -E foo install Django
Requirement already satisfied: Django in /usr/share/pyshared
Installing collected packages: Django
Successfully installed Django

据我所知,pip -E foo install以上内容应该重新安装新版本的Django。另外,如果我告诉pip冻结环境,则会得到很多软件包。我希望对于一个新鲜的环境来说--no-site-packages这是空白?

$ pip -E foo freeze
4Suite-XML==1.0.2
BeautifulSoup==3.1.0.1
Brlapi==0.5.3
BzrTools==1.17.0
Django==1.1
... and so on ...

我是否误解了--no-site-packages应该如何工作?

I was under the impression that virtualenv --no-site-packages would create a completely separate and isolated Python environment, but it doesn’t seem to.

For example, I have python-django installed globally, but wish to create a virtualenv with a different Django version.

$ virtualenv --no-site-packages foo       
New python executable in foo/bin/python
Installing setuptools............done.
$ pip -E foo install Django
Requirement already satisfied: Django in /usr/share/pyshared
Installing collected packages: Django
Successfully installed Django

From what I can tell, the pip -E foo install above is supposed to re-install a new version of Django. Also, if I tell pip to freeze the environment, I get a whole lot of packages. I would expect that for a fresh environment with --no-site-packages this would be blank?

$ pip -E foo freeze
4Suite-XML==1.0.2
BeautifulSoup==3.1.0.1
Brlapi==0.5.3
BzrTools==1.17.0
Django==1.1
... and so on ...

Am I misunderstanding how --no-site-packages is supposed to work?


回答 0

我遇到了这样的问题,直到意识到(早于发现virtualenv),我就开始在.bashrc文件中的PYTHONPATH中添加目录。由于已经过去一年多了,所以我没有马上想到。

I had a problem like this, until I realized that (long before I had discovered virtualenv), I had gone adding directories to the PYTHONPATH in my .bashrc file. As it had been over a year beforehand, I didn’t think of that straight away.


回答 1

您必须确保pip在创建的虚拟环境中而不是全局环境中运行二进制文件。

env/bin/pip freeze

查看测试:

我们使用以下--no-site-packages选项创建virtualenv :

$ virtualenv --no-site-packages -p /usr/local/bin/python mytest
Running virtualenv with interpreter /usr/local/bin/python
New python executable in mytest/bin/python
Installing setuptools, pip, wheel...done.

我们检查freeze新创建的的输出pip

$ mytest/bin/pip freeze
argparse==1.3.0
wheel==0.24.0

但是,如果我们使用global pip,那么我们将得到:

$ pip freeze
...
pyxdg==0.25
...
range==1.0.0
...
virtualenv==13.1.2

即,pip已在整个系统中安装的所有软件包。通过检查,which pip我们得到了(至少在我的情况下)类似的东西/usr/local/bin/pip,这意味着当我们这样做时pip freeze,将调用此二进制文件而不是mytest/bin/pip

You have to make sure you are running the pip binary in the virtual environment you created, not the global one.

env/bin/pip freeze

See a test:

We create the virtualenv with the --no-site-packages option:

$ virtualenv --no-site-packages -p /usr/local/bin/python mytest
Running virtualenv with interpreter /usr/local/bin/python
New python executable in mytest/bin/python
Installing setuptools, pip, wheel...done.

We check the output of freeze from the newly created pip:

$ mytest/bin/pip freeze
argparse==1.3.0
wheel==0.24.0

But if we do use the global pip, this is what we get:

$ pip freeze
...
pyxdg==0.25
...
range==1.0.0
...
virtualenv==13.1.2

That is, all the packages that pip has installed in the whole system. By checking which pip we get (at least in my case) something like /usr/local/bin/pip, meaning that when we do pip freeze it is calling this binary instead of mytest/bin/pip.


回答 2

最终,我发现无论出于什么原因,pip -E都无法正常工作。但是,如果我实际上激活了virtualenv,并使用virtualenv提供的easy_install来安装pip,然后直接从内部使用pip,那么它似乎可以正常工作,并且只显示virtualenv中的软件包。

Eventually I found that, for whatever reason, pip -E was not working. However, if I actually activate the virtualenv, and use easy_install provided by virtualenv to install pip, then use pip directly from within, it seems to work as expected and only show the packages in the virtualenv


回答 3

我知道这是一个非常老的问题,但是对于那些来到这里寻求解决方案的人来说:

运行之前不要忘记激活virtualenvsource bin/activatepip freeze。否则,您将获得所有全局软件包的列表。

I know this is a very old question but for those arriving here looking for a solution:

Don’t forget to activate the virtualenv (source bin/activate) before running pip freeze. Otherwise you’ll get a list of all global packages.


回答 4

暂时清除PYTHONPATH带有:

export PYTHONPATH=

然后创建并激活虚拟环境:

virtualenv foo
. foo/bin/activate

只有这样:

pip freeze

Temporarily clear the PYTHONPATH with:

export PYTHONPATH=

Then create and activate the virtual environment:

virtualenv foo
. foo/bin/activate

Only then:

pip freeze

回答 5

--no-site-packages顾名思义,应该从中删除标准site-packages目录sys.path。保留在标准Python路径中的所有其他内容都将保留在那里。

--no-site-packages should, as the name suggests, remove the standard site-packages directory from sys.path. Anything else that lives in the standard Python path will remain there.


回答 6

如果直接调用脚本script.py,则在Windows 上可能会发生类似的问题,因为脚本随后使用Windows默认打开器并在虚拟环境之外打开Python。调用它将python script.py在虚拟环境中使用Python。

A similar problem can occur on Windows if you call scripts directly as script.py which then uses the Windows default opener and opens Python outside the virtual environment. Calling it with python script.py will use Python with the virtual environment.


回答 7

当您将virtualenv目录移动到另一个目录(在linux上)或重命名父目录时,似乎也会发生这种情况。

This also seems to happen when you move the virtualenv directory to another directory (on linux), or rename a parent directory.


回答 8

我遇到了同样的问题。对我来说(在Ubuntu上)的问题是我的路径名包含$。当我在$ dir之外创建一个virtualenv时,它工作正常。

奇怪的。

I was having this same problem. The issue for me (on Ubuntu) was that my path name contained $. When I created a virtualenv outside of the $ dir, it worked fine.

Weird.


回答 9

virtualenv pip无法正常工作的可能原因之一是,如果任何父文件夹的名称中都有空格/Documents/project name/app 重命名以/Documents/projectName/app解决该问题。

One of the possible reasons why virtualenv pip won’t work is if any of the parent folders had space in its name /Documents/project name/app renaming it to /Documents/projectName/app solves the problem.


回答 10

我遇到了同样的问题,即venv中的点仍然可以用作全局点。
搜索很多页面后,我以这种方式解决了。
1.通过virtualenv使用选项“ –no-site-packages”创建一个新的venv

virtualenv --no-site-packages --python=/xx/xx/bin/python my_env_nmae

请注意,尽管从Virtualenv的doc文件中的1.7.0版本开始,“-no-site-packages”选项默认为true,但是我发现它除非您手动将其设置为不起作用。为了获得纯净的venv,我强烈建议打开2。激活您创建的新env

source ./my_env_name/bin/activate
  1. 检查您的pip位置和python位置,并确保这两个命令在虚拟环境下
pip --version
which python
  1. 在虚拟环境下使用pip安装不受全局软件包中断影响的软件包
pip install package_name

希望这个答案对您有帮助!

I came accross the same problem where pip in venv still works as global pip.
After searching many pages, i figure it out this way.
1. Create a new venv by virtualenv with option “–no-site-packages”

virtualenv --no-site-packages --python=/xx/xx/bin/python my_env_nmae

please note that although the “–no-site-packages” option was default true since 1.7.0 in the doc file of virtualenv, but i found it not working unless you set it on manually. In order to get a pure venv, i strongly suggest turning this option on 2. Activate the new env you created

source ./my_env_name/bin/activate
  1. Check your pip location and python location and make sure these two commands are under virtual envirement
pip --version
which python
  1. Use pip under virtual env to install packages free from the global packages interuption
pip install package_name

Wish this answer helps you!


回答 11

这是所有pip安装选项的列表-我没有找到任何’ -E‘选项,可能是较旧的版本。下面,我virtualenv将为即将到来的SO用户提供简单的英语用法和使用方法。


一切似乎都不错,请接受激活virtualenvfoo)。它所要做的就是允许我们拥有多个(和不同的)Python环境,即各种Python版本,各种Django版本或任何其他Python包-如果我们有生产中的先前版本,并且想用我们的测试最新的Django版本应用。

简而言之,创建和使用(激活)虚拟环境(virtualenv)使得可以使用不同的Python解释器(即python 2.7和3.3)运行或测试我们的应用程序或简单的python脚本-可以全新安装(使用--no-site-packages选项),也可以使用现有的所有软件包/ last设置(使用--system-site-packages选项)。要使用它,我们必须激活它:

$ pip install django 将其安装到全局站点程序包中,并类似地获取 pip freeze will会给出全局站点程序包的名称。

而在venv dir(foo)内部执行$ source /bin/activate将激活venv,即,现在使用pip安装的所有内容都只会安装在虚拟env中,并且只有现在pip冻结将不会提供全局站点软件包python软件包的列表。一旦激活:

$ virtualenv --no-site-packages foo       
New python executable in foo/bin/python
Installing setuptools............done.
$ cd foo
$ source bin/activate 
(foo)$ pip install django

(foo)$符号表明我们正在使用虚拟python环境之前,即任何带有pip的东西-安装,冻结,卸载将仅限于该venv,并且对全局/默认Python安装/软件包没有影响。

Here’s the list of all the pip install options – I didn’t find any ‘-E‘ option, may be older version had it. Below I am sharing a plain english usage and working of virtualenv for the upcoming SO users.


Every thing seems fine, accept activating the virtualenv (foo). All it does is allow us to have multiple (and varying) python environment i.e. various Python versions, or various Django versions, or any other Python package – in case we have a previous version in production and want to test the latest Django release with our application.

In short creating and using (activating) virtual environment (virtualenv) makes it possible to run or test our application or simple python scripts with different Python interpreter i.e. Python 2.7 and 3.3 – can be a fresh installation (using --no-site-packages option) or all the packages from existing/last setup (using --system-site-packages option). To use it we have to activate it:

$ pip install django will install it into the global site-packages, and similarly getting the pip freeze will give names of the global site-packages.

while inside the venv dir (foo) executing $ source /bin/activate will activate venv i.e. now anything installed with pip will only be installed in the virtual env, and only now the pip freeze will not give the list of global site-packages python packages. Once activated:

$ virtualenv --no-site-packages foo       
New python executable in foo/bin/python
Installing setuptools............done.
$ cd foo
$ source bin/activate 
(foo)$ pip install django

(foo) before the $ sign indicates we are using a virtual python environment i.e. any thing with pip – install, freeze, uninstall will be limited to this venv, and no effect on global/default Python installation/packages.