When a python module (cairo) is installed successfully but fails to import in python, what could be the issue?

痞子三分冷 提交于 2019-12-24 11:27:33

问题


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 "<stdin>", line 1, in <module>
ImportError: No module named cairo

回答1:


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
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

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.



来源:https://stackoverflow.com/questions/24346472/when-a-python-module-cairo-is-installed-successfully-but-fails-to-import-in-py

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