python 没有站点包的 Ubuntu 上的 Virtualenv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/249283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Virtualenv on Ubuntu with no site-packages
提问by Jeremy Cantrell
I've been using virtualenv lately while developing in python. I like the idea of a segregated development environment using the --no-site-packages option, but doing this while developing a PyGTK app can be a bit tricky. The PyGTK modules are installed on Ubuntu by default, and I would like to make a virtualenv (with --no-site-packages) aware of specific modules that are located elsewhere on the system.
我最近在用 python 开发时一直在使用 virtualenv。我喜欢使用 --no-site-packages 选项的隔离开发环境的想法,但是在开发 PyGTK 应用程序时这样做可能有点棘手。PyGTK 模块默认安装在 Ubuntu 上,我想让一个 virtualenv(带有 --no-site-packages)知道位于系统其他地方的特定模块。
What's the best way to do this? Or should I just suck it up and drop the --no-site-packages option?
做到这一点的最佳方法是什么?或者我应该把它吸干并删除 --no-site-packages 选项?
回答by iElectric
$ virtualenv --no-site-packages --python=/usr/bin/python2.6 myvirtualenv
$ cd myvirtualenv
$ source bin/activate
$ cd lib/python2.6/
$ ln -s /usr/lib/pymodules/python2.6/gtk-2.0/
$ ln -s /usr/lib/pymodules/python2.6/pygtk.pth
$ ln -s /usr/lib/pymodules/python2.6/pygtk.py
$ ln -s /usr/lib/pymodules/python2.6/cairo/
$ python
>>> import pygtk
>>> import gtk
回答by monkut
One way is to add the paths to your code using sys.path.
一种方法是使用 sys.path 将路径添加到您的代码中。
import sys
sys.path.append(somepath)
Another way is to use site, which processes .pth files in addition to adding to sys.path.
另一种方法是使用站点,除了添加到 sys.path 之外,它还处理 .pth 文件。
import site
site.addsitedir(sitedir, known_paths=None)
https://docs.python.org/library/site.html
https://docs.python.org/library/site.html
But you probably don't want to add this to all your related code.
但您可能不想将其添加到所有相关代码中。
I've seen mention of sitecustomize.py being used to perform something like this, but after some testing I couldn't get it to work as might be expected.
我已经看到提到 sitecustomize.py 被用来执行这样的事情,但经过一些测试后,我无法让它按预期工作。
Here it mentions that auto-import of sitecustomize.py ended in 2.5, if your not on 2.5 try it out. (just add one of the path add methods above to the file and drop it in the directory your program is run) A work around method is mentioned in the post for users of 2.5 and up.
这里提到 sitecustomize.py 的自动导入在 2.5 结束,如果你不是 2.5 试试吧。(只需将上面的路径添加方法之一添加到文件中,然后将其放到程序运行的目录中) 帖子中为 2.5 及更高版本的用户提到了一种变通方法。
回答by Ali Afshar
I find in this situation, symlinks, or even copying specific files (packages, modules, extensions) works really well.
我发现在这种情况下,符号链接,甚至复制特定文件(包、模块、扩展)都非常有效。
It allows the program to emulate being run in the target environment, rather than changing the application to suit the development environment.
它允许程序模拟在目标环境中运行,而不是更改应用程序以适应开发环境。
Same deal for something like AppEngine.
AppEngine之类的东西也一样。
回答by Shane H
Check out the postmkvirtualenv hook script here:
在此处查看 postmkvirtualenv 钩子脚本:
https://stackoverflow.com/a/9716100/60247
https://stackoverflow.com/a/9716100/60247
In that case, he's using it to import PyQt and SIP after a new Virtualenv is created, but you can add the packages that you need to LIBS.
在这种情况下,他会在创建新的 Virtualenv 后使用它来导入 PyQt 和 SIP,但您可以将所需的包添加到 LIBS。
And vote that script up because it's fantastic :)
并投票该脚本,因为它太棒了:)
回答by Anthon
If you want to include the links to the relevant system's python gtk-2.0 in the virtualenv, you can just use pip to install ruamel.venvgtk:
如果你想在 virtualenv 中包含相关系统的 python gtk-2.0 的链接,你可以使用 pip 安装ruamel.venvgtk:
pip install ruamel.venvgtk You don't have import anything, the links are setup during installation.
pip install ruamel.venvgtk 你没有导入任何东西,链接是在安装过程中设置的。
This is especially handy if you are using tox
, in that case you only need to include the dependency (for tox):
如果您正在使用tox
,这尤其方便,在这种情况下,您只需要包含依赖项(对于 tox):
deps:
pytest
ruamel.venvgtk
and a newly setup python2.7 environment will have the relevant links included before the tests are run.
在运行测试之前,新设置的 python2.7 环境将包含相关链接。
More detailed information on how the links are setup can be found in this answer
可以在此答案中找到有关如何设置链接的更多详细信息