Python package installed globally, but not in a virtualenv (PyGTK)

送分小仙女□ 提交于 2019-11-30 14:19:59

问题


I'm having some strange issues with PyGTK in "virtualenv". gtk does not import in my virtualenv, while it does import in my global python install. (I wasn't having this particular issue last week, guessing some software update upset something.)

Is there a good way to resolve this behavior?

Shown here: importing gtk globally,

tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> gtk
<module 'gtk' from '/usr/lib/pymodules/python2.7/gtk-2.0/gtk/__init__.pyc'>

and then failing to import gtk,

tom@zeppelin:~$ workon py27
(py27)tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named gtk

Unfortunately, this has broken my ipython --pylab environment: http://pastebin.com/mM0ur7Hc

UPDATE: I was able to fix this by adding symbolic links as suggested by grepic / this thread: Python: virtualenv - gtk-2.0

with a minor difference, namely that my "cairo" package was located in /usr/lib/pymodules/python2.7/cairo/ rather than in /usr/lib/python2.7/dist-packages/cairo.

SECOND UPDATE: I also found it useful to add the following lines to my venv/bin/activate:

export PYTHONPATH=$PYTHONPATH:/home/tom/.virtualenvs/py27/lib/python2.7/dist-packages
export PYTHONPATH=$PYTHONPATH:/home/tom/.virtualenvs/py27/lib/python2.7/dist-packages/gtk-2.0
export PYTHONPATH=$PYTHONPATH:/usr/lib/pymodules/python2.7/gtk-2.0

(I suspect that one or more of these is unneccessary, but I've been fiddling around with this for too long and have decided to stop investigating -- my setup now works and so I'm satisfied.)

Problem solved! Thanks everyone.


回答1:


So gtk normally lives in a place like /usr/lib/python2.7/dist-packages which is in your Python path in your global environment, but not in your virtual environment.

You may wish to just add the path to gtk manually with something like

import sys
sys.path.append("/usr/lib/python2.7/dist-packages/gtk")

You could also change the path when you activate the virtual environment. Open up venv/bin/activate. Its a scary looking file, but at the end you can just put:

export PATH=$PATH:/my/custom/path

Save that and the next time you activate the virtual environment with:

source venv/bin/activate

your custom path will be in the path. You can verify this with

echo $PATH

An alternative approach suggested Python: virtualenv - gtk-2.0 is to go into your virtualenv directory and add a 'dist-packages' directory and create symbolic links to the gtk package you were using previously:

mkdir -p venv/lib/python2.7/dist-packages/
cd venv/lib/python2.7/dist-packages/

For GTK2:

ln -s /usr/lib/python2.7/dist-packages/glib/ glib
ln -s /usr/lib/python2.7/dist-packages/gobject/ gobject
ln -s /usr/lib/python2.7/dist-packages/gtk-2.0* gtk-2.0
ln -s /usr/lib/python2.7/dist-packages/pygtk.pth pygtk.pth
ln -s /usr/lib/python2.7/dist-packages/cairo cairo

For GTK3:

ln -s /usr/lib/python2.7/dist-packages/gi gi

Full disclosure: I feel that both these solutions are somewhat hackish, which is ok given that you say the question is urgent. There is probably a 'proper' way to extend a virtual environment so let us know if you eventually discover the better solution. You may have some luck with http://www.virtualenv.org/en/latest/index.html#creating-your-own-bootstrap-scripts




回答2:


Try creating your virtual environment with the --system-site-packages flag.




回答3:


Another way to do this is to create a .pth file in your virtualenv's site-packages dir

eg

(in <virtualenv>/lib/python2.7/site-packages/dist-packages.pth)

/usr/lib/python2.7/dist-packages/

This fixed the issue I was having with apt-get installed version of pycairo




回答4:


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.




回答5:


It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access PyGTK, do the following:

pip install vext
pip install vext.pygtk


来源:https://stackoverflow.com/questions/12830662/python-package-installed-globally-but-not-in-a-virtualenv-pygtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!