Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python - Installing PyGtk in virtualenv

So I am trying to run a simple matplotlib example in my virtualenv (in the console). Here's the code:

import matplotlib
matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt
radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724]
plt.plot(radius, area)
plt.show()

However, when I run this I get:

ImportError: Gtk* backend requires pygtk to be installed.

And now the fun starts. I tried to pip install pygtk but it throws:

********************************************************************
* Building PyGTK using distutils is only supported on windows. *
* To build PyGTK in a supported way, read the INSTALL file.    *
********************************************************************
Complete output from command python setup.py egg_info:
********************************************************************

I have checked the INSTALL file and says to try ./configfure; make; make install. However. I am not quite sure how to do this within virtualenv. Where do I unpack the sources for pygtk in order to be installed within virtualenv.

question from:https://stackoverflow.com/questions/9064289/installing-pygtk-in-virtualenv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The trick is to manually set the correct paths and then run configure inside the virtualenv. This is quite basic, but it worked for me.

Install python-config in the virtual env and link it to python2.7-config:

pip install config
ln -s /home/PATH/TO/VIRT/bin/python-config /home/PATH/TO/VIRT/bin/python2.7-config

Install cairo in the virtual env:

wget http://cairographics.org/releases/py2cairo-1.10.0.tar.bz2
tar -xf py2cairo-1.10.0.tar.bz2
cd py2cairo-1.10.0
./waf configure --prefix=/home/PATH/TO/VIRT/
./waf build
./waf install

Install PyGTK

wget http://pypi.python.org/packages/source/P/PyGTK/pygtk-2.24.0.tar.bz2
tar -xf pygtk-2.24.0.tar.bz2
cd pygtk-2.24.0
export PKG_CONFIG_PATH=/home/PATH/TO/VIRT/lib/pkgconfig
./configure --prefix=/home/PATH/TO/VIRT/
make 
make install

And that should do it. Just replace PATH/TO/VIRT/ with your own path. I'm sure someone could assist on adding the path to virtualenvwrapper?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...