I've just installed pygtk through Homebrew(awesome tool) as well as its dependences (including gtk+); the thing is when I try to import gtk on the python interpreter it throws an ImportError, which don't happens when importing pygtk or any other module into the interpreter, I can't figure out what's wrong. :S
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
After clarification from the OP, the original answer does not describe his problem… although I think it's related, and it may help other people, so I will leave it. But first, the relevant part.
Before you can
import gtk
, you need to callpygtk.require
, as shown in the Getting Started section of the tutorial. (For more details, see the PyGTK FAQ.)That should solve your problem… but you're just crashing with
Fatal Python error: PyThreadState_Get: no current thread
. There are a number of reasons you can get this, but the most likely in this case is that you're running a C extension (pygtk
/gtk
/whatever) built for one version of Python against a different version of Python. (This is actually covered in the PyGTK FAQ as well, but in this case it's a more general issue with CPython C extension modules, not something PyGTK specific.)If you have multiple Python 2.7 versions (and if you have 2.7.3, you very definitely have two, because Apple's is 2.7.2), you have to figure out which ones you have, and which one you used to build
pygtk
, and make sure you use it from the same one.And that takes us back to the original answer, which I've left below.
There are two reasons you can have this problem.
First, if you haven't installed any extra Python versions besides the ones that came with OS X, you may not have read the instructions that
brew
prints out aboutpygtk
whenever you install it, or info it, etc.:For example:
Homebrew installs Python packages into /usr/local/lib/pythonX.Y—even if it's installing for Apple's Python installation. But Apple's Python installation doesn't look for packages there by default. So, you need to do what this says.
Just typing this line at the Terminal will work for the rest of that terminal session. If you want to make it permanent… well, there are many options, depending on exactly what you want. See this answer for details.
This is explained in more detail at Gems, Eggs and Perl Modules and Homebrew and Python.
(This is one of the multiple reasons why Homebrew usually recommends using
pip
whenever possible, rather than looking for packages inbrew
. But there are a few exceptions, and I could easily believe pygtk is one of them.)Second, if you installed Homebrew's Python, you probably don't have your PATH set up right, so you're installing packages into the Homebrew Python's site packages, but then running the Apple Python installation instead.
If you're just typing
python
and don't know which one you're running,which python
should tell you. If it's/usr/bin/python
it's Apple's; if it's/usr/local/bin/python
, that's Homebrew's.You can also tell from the Python startup banner, because Mountain Lion comes with 2.7.2, while Homebrew will install 2.7.3. (Also, IIRC, Apple used a prerelease clang to build their Python, while anything you build with Homebrew will be built with whatever clang/gcc/llvm-gcc you have, which is not going to be a prerelease build…)
NOTE: Just because you see the word "Apple" somewhere in the banner doesn't mean it's Apple's Python! If you build a Homebrew Python, or install a python.org Python, it will almost certainly be built with Apple GCC, Apple Clang, or Apple LLVM-GCC. And it's running on an Apple OS, too. So, "Apple" or "apple" will appear up to three times in the banner for any Python, Apple or not.
The fix for this is to run the Python you want to run. For example:
virtualenv
to create one or more virtual environments based on either or both installations, and pick and choose using the venv tools./usr/local/bin/python
to run Homebrew's Python,/usr/bin/python
to run Apple's. (And modify the shebang lines in any of your scripts to do the same.)PATH
variable to make sure whichever one you want first comes first. For Homebrew, you want either /usr/local/Cellar/python/2.7.3/bin (for just Python) or /usr/local/bin (for everything) before /usr/bin; for Apple, the other way around.All of this applies to other tools. Many people get into trouble by having, e.g., one installation's
pip
oridle
come first in the path, while a different installation'spython
does…If you've installed a different Python, e.g., by using the installers from python.org, you could easily have both of the above problems. For example, you could be installing stuff for python.org Python, but running Apple Python, and on top of that, neither one may be configured to look in the Homebrew site-packages directory.