简体   繁体   中英

No module named pygtk in Python 2.7 (win 32) but it's definitely installed

I have installed a number of python packages using a mix of setup.py install and pip install from the command line. I can successfully import all of these into IDLE. I am running Python 2.7.9 (win32 on a win64 machine).

I installed pygtk 2.22 using pip install--exactly the same as about half a dozen other packages. But I now cannot import it into IDLE (import pygtk all lowercase) and get the typical "No module named pygtk" error. I checked using pip.get_installed_distributions() and it is listed.

Any help?

OK, I think I have one solution for anyone looking for a way to get pygtk:

Get the executable from http://www.www.pygtk.org (I previously used the whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip - these mostly seem pretty good but no luck for me in this case). I used pygtk-2.24.0.win32-py2.7.exe to match my system.

Follow FLIR31207's excellent instructions regarding adding to the system path. For me this was:

import sys
sys.path.append("C:\Python27\Lib\site-packages\gtk-2.0")

import pygtk will still throw an error, but a different error (which I can't now reproduce because I fixed it). Something like missing pygobject.

Turns out pygtk has dependencies. Back to pygtk.org to get PyCairo and PyGObject executables and install these as well.

import pygtk and no error. I think that's it. Thanks FLIR31207!

If you import a module with import modulexyz Python searches for a file called modulexyz in

  • the current folder
  • all folders in sys.path

If pygtk.py is not in one of those, it cannot be imported with a simple import pygtk . I suggest doing the following:

  • run python
  • in this interactive Python session run
    • import sys
    • print sys.path

This will print a (long) list of folders where Python will look for modules; if the pygtk.py cannot be found in one of those, you can add it to sys.path before importing the module:

import sys
sys.path.append(folder_where_pygtk.py_is_located)
import pygtk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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