When a python module is installed successfully but fails to import in python (via PyCharm IDE), what could be the issue? What checklist should I follow to diagnose the problem in this case and in the future when a module installs but later fails to import?
For this specific case, I installed a vector graphics module for Python called cairo.
http://cairographics.org/releases/pycairo-1.8.8.tar.gz
See the successful install below:
sudo python setup.py install
cairo >= 1.8.8 detected
creating pycairo.pc
creating src/config.h
running install
running build
running build_ext
running install_lib
creating /Library/Python/2.7/site-packages/cairo
copying build/lib.macosx-10.9-intel-2.7/cairo/_cairo.so -> /Library/Python/2.7/site-packages/cairo
running install_data
creating /System/Library/Frameworks/Python.framework/Versions/2.7/include/pycairo
copying src/pycairo.h -> /System/Library/Frameworks/Python.framework/Versions/2.7/include/pycairo
copying pycairo.pc -> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/pkgconfig
running install_egg_info
Writing /Library/Python/2.7/site-packages/pycairo-1.8.8-py2.7.egg-info
Everything seemed to proceed smoothly, but later this module failed to import when called for in Python: ImportError: No module named cairo.
One possibility is that it was installed in the wrong directory - /Library/Python/2.7/site-packages/.
Another possibility is that I should be somehow telling python the location of the module explicitly. i.e.: /Library/Python/2.7/site-packages/cairo, based on the output of the install script found above.
It is quite odd, because cairo is present in /Library/Python/2.7/site-packages/, and I am able to import other libraries such as PIL found in the same directory.
Based on aIKID's suggestion, I tried using pip to get the installed packages. From this, I got a long list, including a module called pycairo. I tried importing both pycairo and cairo and still got an import error. It's interesting that "cairo" is found in /Library/Python/2.7/site-packages/cairo, but "pycairo" is found after a call of pip.get_installed_distributions(). It seems that the distribution is called pycairo, and the module is called cairo. import cairo should work, but either way both imports fail.
Based on Christopher's suggestion, I checked the /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ directory and did not find cairo. By the same token, I did not find PIL (the python imaging library). I am still able to import PIL, which is found in /Library/Python/2.7/site-packages/, together with cairo.
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL, cairo
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named cairo
解决方案
You can run python interactive mode and simply do the following steps to confirm your lib paths:
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path
As you can see, /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ is the path in my system.
You might have multiple versions of python, but you can confirm which one you are using by doing this.