Ubuntu 上的 Virtualenv 没有站点包

发布于 2024-07-08 19:27:00 字数 266 浏览 9 评论 0原文

我最近在使用 python 进行开发时一直在使用 virtualenv。 我喜欢使用 --no-site-packages 选项的隔离开发环境的想法,但在开发 PyGTK 应用程序时执行此操作可能有点棘手。 PyGTK 模块默认安装在 Ubuntu 上,我想让 virtualenv (使用 --no-site-packages)了解位于系统其他位置的特定模块。

最好的方法是什么? 或者我应该接受它并放弃 --no-site-packages 选项?

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.

What's the best way to do this? Or should I just suck it up and drop the --no-site-packages option?

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

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

发布评论

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

评论(5

み零 2024-07-15 19:27:00
$ 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
$ 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
2024-07-15 19:27:00

一种方法是使用 sys.path 将路径添加到代码中。

import sys

sys.path.append(somepath)

另一种方法是使用 site,它除了添加到 sys.path 之外还处理 .pth 文件。

import site

site.addsitedir(sitedir, known_paths=None)

https://docs.python.org/library/site.html

但你可能不想将其添加到所有相关代码中。

我见过提到 sitecustomize.py 被用来执行类似的操作,但经过一些测试后,我无法让它按预期工作。

这里提到了 sitecustomize.py 的自动导入在 2.5 中结束,如果你不是 2.5 的话可以尝试一下。 (只需将上面的路径添加方法之一添加到文件中并将其放入程序运行的目录中即可)
帖子中提到了针对 2.5 及更高版本用户的解决方法。

http://code.activestate.com/recipes/552729/

One way is to add the paths to your code using 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.

import site

site.addsitedir(sitedir, known_paths=None)

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.

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.

http://code.activestate.com/recipes/552729/

日暮斜阳 2024-07-15 19:27:00

我发现在这种情况下,符号链接,甚至复制特定文件(包、模块、扩展)都非常有效。

它允许程序模拟在目标环境中运行,而不是更改应用程序以适应开发环境。

对于 AppEngine 之类的东西也是如此。

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.

诠释孤独 2024-07-15 19:27:00

在此处查看 postmkvirtualenv 挂钩脚本:

https://stackoverflow.com/a/9716100/60247

在这种情况下,他是创建新的 Virtualenv 后,可以使用它导入 PyQt 和 SIP,但您可以将所需的包添加到 LIBS。

并投票支持该脚本,因为它太棒了:)

Check out the postmkvirtualenv hook script here:

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.

And vote that script up because it's fantastic :)

淡写薰衣草的香 2024-07-15 19:27:00

如果你想在 virtualenv 中包含相关系统的 python gtk-2.0 的链接,你可以使用 pip 安装 ruamel.venvgtk:

pip install ruamel.venvgtk
您无需导入任何内容,链接是在安装过程中设置的。

如果您使用 tox,这尤其方便,在这种情况下,您只需要包含依赖项(对于 tox):

deps:
    pytest
    ruamel.venvgtk

并且新设置的 python2.7 环境将在测试之前包含相关链接跑步。

有关如何设置链接的更多详细信息,请参阅此答案

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:

pip install ruamel.venvgtk
You don't have import anything, the links are setup during installation.

This is especially handy if you are using tox, in that case you only need to include the dependency (for tox):

deps:
    pytest
    ruamel.venvgtk

and a newly setup python2.7 environment will have the relevant links included before the tests are run.

More detailed information on how the links are setup can be found in this answer

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