简体   繁体   English

Ubuntu上的Virtualenv没有站点包

[英]Virtualenv on Ubuntu with no site-packages

I've been using virtualenv lately while developing in python. 我最近在使用python进行开发时一直在使用virtualenv。 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. 我喜欢使用--no-site-packages选项的隔离开发环境的想法,但在开发PyGTK应用程序时这样做可能有点棘手。 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. 默认情况下,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选项?

$ 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

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. 另一种方法是使用site,除了添加到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. 在这里,它提到sitecustomize.py的自动导入以2.5结尾,如果你没有在2.5上尝试它。 (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. (只需将上面的路径添加方法之一添加到文件中并将其放在程序运行的目录中)在2.5及以上的用户的帖子中提到了一种解决方法。

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

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 :) 投票那个剧本,因为它太棒了:)

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这样的东西相同。

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 有关如何设置链接的更多详细信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM