问题:使用virtualenv恢复`–no-site-packages`选项

我使用该--no-site-packages选项创建了virtualenv 并安装了许多库。现在,我想还原该--no-site-packages选项并使用全局包。

我可以不重新创建virtualenv来做到吗?

更确切地说:

我不知道究竟创建使用一个的virtualenv发生时,--no-site-packages相对于没有使用该选项的选择。

如果我知道会发生什么,那么我可以弄清楚如何撤消它。

I have created a virtualenv using the --no-site-packages option and installed lots of libraries. Now I would like to revert the --no-site-packages option and use also the global packages.

Can I do that without recreating the virtualenv?

More precisely:

I wonder what exactly happens when creating a virtualenv using the --no-site-packages option as opposed to not using that option.

If I know what happens then I can figure out how to undo it.


回答 0

尝试删除(或重命名)虚拟环境下文件夹no-global-site-packages.txt中的Lib文件。

其中venv是您的虚拟环境的名称,而python3.4对应于所涉及的任何python版本,例如:

$ rm venv/lib/python3.4/no-global-site-packages.txt

如果您改变主意并想放回原处:

$ touch venv/lib/python3.4/no-global-site-packages.txt

注意:如果看不到上述文件,则说明您有较新版本的virtualenv。你要遵循这个答案,而不是

Try removing (or renaming) the file no-global-site-packages.txt in your Lib folder under your virtual environment.

Where venv is the name of your virtual environment, and python3.4 corresponds to whichever version of python involved, for example:

$ rm venv/lib/python3.4/no-global-site-packages.txt

And if you change your mind and want to put it back:

$ touch venv/lib/python3.4/no-global-site-packages.txt

Note: If you don’t see the above file, then you have a newer version of virtualenv. You’ll want to follow this answer instead


回答 1

至少对于Python 3.5.2,pyvenv.cfg在virtualenv目录的根目录中有一个文件。您需要做的就是将include-system-site-packages标志从更改falsetrue

home = /usr/bin
include-system-site-packages = false  # <- change this to "true"
version = 3.5.2

At least for Python 3.5.2, there is pyvenv.cfg file in the root of virtualenv directory. All you need to do is to change include-system-site-packages flag from false to true:

home = /usr/bin
include-system-site-packages = false  # <- change this to "true"
version = 3.5.2

回答 2

使用virtualenvwrapper管理virtualenvs时,可以使用shell函数toggleglobalsitepackages在使用和不使用站点包之间进行切换。

When using virtualenvwrapper to manage virtualenvs, you can use the shell function toggleglobalsitepackages to switch between using and not using site packages.


回答 3

尝试在/virtualenv_root/lib/和之间添加符号链接/path/to/desired/site-packages/

Try adding a symlink between /virtualenv_root/lib/ and /path/to/desired/site-packages/


回答 4

转到您的venv文件夹并打开pyvenv.cfg。(例如,如果您的虚拟环境被调用,myenv则该文件将位于myenv\pyvenv.cfg

您会看到一个布尔设置,名为 include-system-site-packages

将该设置设置true为使用全局包

如果要禁用使用全局软件包,只需将该设置设置为即可false

Go to your venv folder and open pyvenv.cfg. (E.g. if your virtual environment is called myenv then the file will be located at myenv\pyvenv.cfg)

You’ll see a boolean setting called include-system-site-packages

Set that setting to true to use global packages

If you want to disable using global packages, just set that setting to false instead.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。