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
应该如何工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我遇到了这样的问题,直到我意识到(早在我发现 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.
您必须确保在您创建的虚拟环境中运行
pip
二进制文件,而不是全局环境。查看测试:
我们使用
--no-site-packages
选项创建 virtualenv:我们检查新创建的
pip
的freeze
输出>:但是如果我们确实使用全局
pip
,这就是我们得到的:即
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.See a test:
We create the virtualenv with the
--no-site-packages
option:We check the output of
freeze
from the newly createdpip
:But if we do use the global
pip
, this is what we get:That is, all the packages that
pip
has installed in the whole system. By checkingwhich pip
we get (at least in my case) something like/usr/local/bin/pip
, meaning that when we dopip freeze
it is calling this binary instead ofmytest/bin/pip
.最终我发现,无论出于何种原因,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
我知道这是一个非常古老的问题,但对于那些来到这里寻找解决方案的人来说:
在运行
激活 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 runningpip freeze
. Otherwise you'll get a list of all global packages.暂时清除
PYTHONPATH
:然后创建并激活虚拟环境:
然后:
Temporarily clear the
PYTHONPATH
with:Then create and activate the virtual environment:
Only then:
--no-site-packages
顾名思义,应该从sys.path
中删除标准 site-packages 目录。标准 Python 路径中的任何其他内容都将保留在那里。--no-site-packages
should, as the name suggests, remove the standard site-packages directory fromsys.path
. Anything else that lives in the standard Python path will remain there.如果您直接以
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 withpython script.py
will use Python with the virtual environment.当您将 virtualenv 目录移动到另一个目录(在 Linux 上)或重命名父目录时,似乎也会发生这种情况。
This also seems to happen when you move the virtualenv directory to another directory (on linux), or rename a parent directory.
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.这是所有 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 包列表。一旦激活:$
符号之前的(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 ofvirtualenv
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 thepip 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:(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.我也遇到了同样的问题。对我来说(在 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.
我遇到了同样的问题,venv 中的 pip 仍然可以用作全局 pip。
搜索了很多页后,我找到了这样的方法。
1.通过virtualenv使用选项“--no-site-packages”创建一个新的venv
请注意,虽然virtualenv的doc文件中的“--no-site-packages”选项从1.7.0开始默认为true,但是我发现它不起作用,除非您手动设置它。为了获得纯粹的venv,我强烈建议打开此选项
2.激活您创建的新环境
希望这个答案对您有帮助!
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"
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
Wish this answer helps you!
我的问题是 pip 和 python3 版本。对于最新版本的
django
安装,pip3
是必需的。所以使用以下命令创建虚拟环境后我的问题就解决了:PS 出现这个问题是因为我的ubuntu中的python默认版本是2.7。通过使用上述命令,它将忽略默认版本。
My problem was the
pip
andpython3
versions. For the latest version ofdjango
installation,pip3
is necessary. So my problem was solved after creating the virtual environment using the following commands: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.
对于更新到 ubuntu 22.04 或因相同问题而困惑的任何人,请先删除已安装的 pip,然后将 pip 重新安装到 /usr/local/bin,根据 python.org 中的文档,时间为 3.10.5,运行:
如果没有,则需要下载脚本。返回成功消息后,安装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:
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.
我的问题是我已经安装了虚拟环境,如下所示然后
激活 venv 后,我正在运行
它仅依赖于全局包,因为它没有提示我安装包。
运行下面的代码就成功了
My issue was that I had installed virtual env as followed
Then after activating the venv, I was running
Which just relied on global packages, as it wasn't prompting me to install packages.
Running the below did the trick