virtualenv --no-site-packages 和 pip 仍然找到全局包?

发布于 2024-08-03 20:55:13 字数 812 浏览 6 评论 0 原文

我的印象是 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

久夏青 2024-08-10 20:55:13

我遇到了这样的问题,直到我意识到(早在我发现 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.

沫尐诺 2024-08-10 20:55:13

您必须确保在您创建的虚拟环境中运行 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.

我们检查新创建的 pipfreeze 输出>:

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

但是如果我们确实使用全局pip,这就是我们得到的:

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

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

只有一腔孤勇 2024-08-10 20:55:13

最终我发现,无论出于何种原因,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

清音悠歌 2024-08-10 20:55:13

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

在运行 激活 virtualenv (source bin/activate) >点冻结。否则您将获得所有全局包的列表。

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.

悲喜皆因你 2024-08-10 20:55:13

暂时清除 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
云胡 2024-08-10 20:55:13

--no-site-packages 顾名思义,应该从 sys.path 中删除标准 site-packages 目录。标准 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.

会发光的星星闪亮亮i 2024-08-10 20:55:13

如果您直接以 script.py 形式调用脚本,然后使用 Windows 默认打开程序并在虚拟环境之外打开 Python,那么在 Windows 上也会出现类似的问题。使用 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.

趁年轻赶紧闹 2024-08-10 20:55:13

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

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

三生殊途 2024-08-10 20:55:13

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.

素衣风尘叹 2024-08-10 20:55:13

这是所有 pip install 选项 的列表 - 我没有找到任何“-E”选项,可能是旧版本有它。下面我为即将到来的 SO 用户分享了 virtualenv 的简单英语用法和工作原理。


一切看起来都很好,接受激活 virtualenv (foo)。它所做的只是允许我们拥有多个(且不同的)Python 环境,即各种 Python 版本,或各种 Django 版本,或任何其他 Python 包 - 以防我们在生产环境中使用以前的版本并希望使用我们的版本来测试最新的 Django 版本。应用。

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

$ pip install django 会将其安装到全局站点包中,同样获取 pip freeze 将会给出全局站点的名称- 包。

在 venv dir (foo) 中执行 $ source /bin/activate 将激活 venv,即现在使用 pip 安装的任何内容都只会安装在虚拟环境中,并且现在 pip freeze 不会给出全局站点包 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.

怎言笑 2024-08-10 20:55:13

我也遇到了同样的问题。对我来说(在 Ubuntu 上)的问题是我的路径名包含 $。当我在 $ 目录之外创建一个 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.

昔日梦未散 2024-08-10 20:55:13

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

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

请注意,虽然virtualenv的doc文件中的“--no-site-packages”选项从1.7.0开始默认为true,但是我发现它不起作用,除非您手动设置它。为了获得纯粹的venv,我强烈建议打开此选项
2.激活您创建的新环境

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!

指尖微凉心微凉 2024-08-10 20:55:13

我的问题是 pip 和 python3 版本。对于最新版本的django安装,pip3是必需的。所以使用以下命令创建虚拟环境后我的问题就解决了:

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

PS 出现这个问题是因为我的ubuntu中的python默认版本是2.7。通过使用上述命令,它将忽略默认版本。

My problem was the pip and python3 versions. For the latest version of django installation, pip3 is necessary. 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. By using the above command it would ignore the default version.

勿挽旧人 2024-08-10 20:55:13

对于更新到 ubuntu 22.04 或因相同问题而困惑的任何人,请先删除已安装的 pip,然后将 pip 重新安装到 /usr/local/bin,根据 python.org 中的文档,时间为 3.10.5,运行:

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

如果没有,则需要下载脚本。返回成功消息后,安装virtualenv或poetry,它会注意到/usr/local不可写,并会安装到~/.local/what/ever ,那很好。然后一切都很好,使用 virtualenv 在项目目录中重新创建 venv 文件夹。
如果发生任何奇怪的情况,请使用which命令检查pip3或virtualenv的路径,并将其删除。

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:

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. And then everything is good to go, use virtualenv to re-create the venv folder in your project dir.
if anything strange happend, use which command to check the path of pip3 or virtualenv, and remove them.

橘虞初梦 2024-08-10 20:55:13

我的问题是我已经安装了虚拟环境,如下所示然后

python3 -m venv venv

激活 venv 后,我正在运行

python3 main.py

它仅依赖于全局包,因为它没有提示我安装包。

运行下面的代码就成功了

python main.py

My issue was that I had installed virtual env as followed

python3 -m venv venv

Then after activating the venv, I was running

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文