简体   繁体   English

virtualenv --no-site-packages 和 pip 仍在寻找全局包?

[英]virtualenv --no-site-packages and pip still finding global 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.我的印象是virtualenv --no-site-packages会创建一个完全独立和隔离的 Python 环境,但它似乎没有。

For example, I have python-django installed globally, but wish to create a virtualenv with a different Django version.例如,我在全局安装了 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

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.据我所知,上面的pip -E foo install应该会重新安装 Django 的新版本。此外,如果我告诉 pip 冻结环境,我会得到很多包。 I would expect that for a fresh environment with --no-site-packages this would be blank?我希望对于带有--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 ...

Am I misunderstanding how --no-site-packages is supposed to work?我是否误解了--no-site-packages应该如何工作?

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.我遇到了这样的问题,直到我意识到(早在我发现 virtualenv 之前),我已经将目录添加到我的 .bashrc 文件中的 PYTHONPATH 中。 As it had been over a year beforehand, I didn't think of that straight away.因为已经过去一年多了,所以我没有马上想到这一点。

You have to make sure you are running the pip binary in the virtual environment you created, not the global one.您必须确保在您创建的虚拟环境中运行pip二进制文件,而不是在全局环境中运行。

env/bin/pip freeze

See a test:看一个测试:

We create the virtualenv with the --no-site-packages option:我们使用--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.

We check the output of freeze from the newly created pip :我们从新创建的pip检查freeze的输出:

$ 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 ,这就是我们得到的:

$ 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.也就是整个系统中pip已经安装的所有包。 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 .通过检查我们得到(至少在我的情况下) which pip类似/usr/local/bin/pip的东西,这意味着当我们执行pip freeze时,它​​调用的是这个二进制文件而不是mytest/bin/pip

Eventually I found that, for whatever reason, pip -E was not working.最终我发现,无论出于何种原因, pip -E 都不起作用。 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但是,如果我真的激活了virtualenv,并使用virtualenv提供的easy_install来安装pip,然后直接从内部使用pip,它似乎可以按预期工作并且只显示virtualenv中的包

Temporarily clear the PYTHONPATH with:使用以下命令暂时清除PYTHONPATH

export PYTHONPATH=

Then create and activate the virtual environment:然后创建并激活虚拟环境:

virtualenv foo
. foo/bin/activate

Only then:只有这样:

pip 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 .在运行pip freeze之前不要忘记激活 virtualenv ( source bin/activate )。 Otherwise you'll get a list of all global packages.否则,您将获得所有全局包的列表。

--no-site-packages should, as the name suggests, remove the standard site-packages directory from sys.path .顾名思义, --no-site-packages应该从sys.path中删除标准的 site-packages 目录。 Anything else that lives in the standard Python path will remain there.任何其他存在于标准 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.如果您直接将脚本调用为script.py ,则在 Windows 上可能会出现类似的问题,然后使用 Windows 默认打开器并在虚拟环境之外打开 Python。 Calling it with python script.py will use Python with the virtual environment.使用python script.py调用它会在虚拟环境中使用 Python。

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

I was having this same problem.我遇到了同样的问题。 The issue for me (on Ubuntu) was that my path name contained $ .我(在 Ubuntu 上)的问题是我的路径名包含$ When I created a virtualenv outside of the $ dir, it worked fine.当我在 $ 目录之外创建一个 virtualenv 时,它运行良好。

Weird.诡异的。

virtualenv pip 不起作用的可能原因之一是,如果任何父文件夹的名称/Documents/project name/app中有空格,将其重命名为/Documents/projectName/app可以解决问题。

Here's the list of all the pip install options - I didn't find any ' -E ' option, may be older version had it.这是所有 pip 安装选项的列表 - 我没有找到任何 ' -E ' 选项,可能是旧版本有它。 Below I am sharing a plain english usage and working of virtualenv for the upcoming SO users.下面我为即将到来的 SO 用户分享一个简单的英语用法和virtualenv的工作。


Every thing seems fine, accept activating the virtualenv ( foo ).一切似乎都很好,接受激活virtualenv ( foo )。 All it does is allow us to have multiple (and varying) python environment ie 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.它所做的只是允许我们拥有多个(和不同的)python 环境,即各种 Python 版本,或各种 Django 版本,或任何其他 Python 包 - 以防我们在生产中有以前的版本并想用我们的测试最新的 Django 版本应用。

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 ie 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).简而言之,创建和使用(激活)虚拟环境( virtualenv )可以使用不同的 Python 解释器(即 Python 2.7 和 3.3)运行或测试我们的应用程序或简单的 Python 脚本 - 可以是全新安装(使用--no-site-packages选项)或现有/上次设置中的所有软件包(使用--system-site-packages选项)。 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. $ pip install django会将其安装到全局站点包中,同样获得pip freeze将给出全局站点包的名称。

while inside the venv dir (foo) executing $ source /bin/activate will activate venv ie 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.而在 venv dir (foo) 中执行$ source /bin/activate将激活 venv 即现在用 pip 安装的任何东西都只会安装在虚拟环境中,只有现在 pip freeze 不会给出全局站点包列表 python包。 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 ie any thing with pip - install, freeze, uninstall will be limited to this venv, and no effect on global/default Python installation/packages. (foo)$符号之前表示我们正在使用虚拟 Python 环境,即任何带有 pip 的东西 - 安装、冻结、卸载都将仅限于此 venv,并且对全局/默认 Python 安装/包没有影响。

I came accross the same problem where pip in venv still works as global pip.我遇到了同样的问题,venv 中的 pip 仍然可以作为全局 pip。
After searching many pages, i figure it out this way.在搜索了很多页面后,我以这种方式弄清楚了。
1. Create a new venv by virtualenv with option "--no-site-packages" 1. 通过 virtualenv 使用选项“--no-site-packages”创建一个新的 venv

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.请注意,虽然“--no-site-packages”选项自 1.7.0 起在 virtualenv 的 doc 文件中默认为 true,但我发现它不起作用,除非您手动设置它。 In order to get a pure venv, i strongly suggest turning this option on 2. Activate the new env you created为了得到一个纯 venv,我强烈建议打开这个选项 2. 激活你创建的新 env

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 位置和 python 位置,并确保这两个命令在虚拟环境下
pip --version
which python
  1. Use pip under virtual env to install packages free from the global packages interuption在 virtual env 下使用 pip 来安装不受全局包中断的包
pip install package_name

Wish this answer helps you!希望这个答案对你有帮助!

My problem was the pip and python3 versions.我的问题是pippython3版本。 For the latest version of django installation, pip3 is necessary.对于最新版本的django安装, pip3是必需的。 So my problem was solved after creating the virtual environment using the following commands:因此,在使用以下命令创建虚拟环境后,我的问题得到了解决:

> virtualenv --python=python3 venv
> source venv/bin/activate
> which pip3 #should be different from /usr/local/bin/pip3
...<some-directory>/venv/bin/pip3

PS This problem occurred because the default version of my python in ubuntu was 2.7. PS出现这个问题是因为我在ubuntu中的python默认版本是2.7。 By using the above command it would ignore the default version.通过使用上述命令,它将忽略默认版本。

for anyone updated to ubuntu 22.04 or confused by the same problem, remove the installed pip first, re-install the pip to /usr/local/bin, according to the doc in python.org, by the time is 3.10.5, run:对于任何更新到 ubuntu 22.04 或被相同问题混淆的人,首先删除已安装的 pip,根据 python.org 中的文档将 pip 重新安装到 /usr/local/bin,到时间是 3.10.5,运行:

sudo python get-pip.py --prefix=/usr/

you need to download the script if you don't have one.After success message return, install virtualenv or poetry, it would notice you that /usr/local is not writable, and would install to ~/.local/what/ever, that' fine.如果你没有脚本,你需要下载脚本。返回成功消息后,安装virtualenv或诗歌,它会注意到/usr/local不可写,并安装到~/.local/what/ever,没事。 And then everything is good to go, use virtualenv to re-create the venv folder in your project dir.然后一切顺利,使用 virtualenv 在项目目录中重新创建 venv 文件夹。 if anything strange happend, use which command to check the path of pip3 or virtualenv, and remove them.如果发生任何奇怪的事情,请使用 which 命令检查 pip3 或 virtualenv 的路径,并将其删除。

My issue was that I had installed virtual env as followed我的问题是我已经安装了虚拟环境如下

python3 -m venv venv

Then after activating the venv, I was running然后在激活 venv 之后,我正在运行

python3 main.py

Which just relied on global packages, as it wasn't prompting me to install packages.它只依赖于全局包,因为它没有提示我安装包。

Running the below did the trick运行下面的技巧

python main.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用`--no-site-packages`的VirtualEnv带有空的`pip Frozen`-如何? - VirtualEnv with empty `pip freeze` using `--no-site-packages` — how to? 将全局包包含到使用 --no-site-packages 创建的 virtualenv 中 - Including global package into a virtualenv that has been created with --no-site-packages Virtualenv,no-site-packages,sys.path - Virtualenv, no-site-packages, sys.path 使用 virtualenv 恢复 `--no-site-packages` 选项 - Revert the `--no-site-packages` option with virtualenv virtualenv --no-site-packages对我不起作用 - virtualenv --no-site-packages is not working for me 为什么 pip 冻结报告使用 --no-site-packages 创建的新虚拟环境中的一些包? - Why does pip freeze report some packages in a fresh virtualenv created with --no-site-packages? 使用--no-site-packages在virtualenv内安装pyopencv - Install pyopencv inside virtualenv with --no-site-packages 安装 virtualenv:无法识别的 arguments:--no-site-packages - Installing virtualenv: unrecognized arguments: --no-site-packages mkvirtualenv --no-site-packages仍然在新环境中找到并安装全局软件包 - mkvirtualenv --no-site-packages still finds and installs global packages in new environment virtualenv virtualenvwrapper virtualenv:错误:无法识别的参数:--no-site-packages - virtualenv virtualenvwrapper virtualenv: error: unrecognized arguments: --no-site-packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM