2

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.

enter image description here

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.

enter image description here

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
user391339
  • 6,908
  • 9
  • 46
  • 67
  • No, i think it's supposed to be like that. Have you restarted the shell? – aIKid Jun 21 '14 at 22:54
  • does restart the shell mean start a new instance of python? I restarted the pycharm IDE w/ no luck, and I started a new instance of oython in the terminal, no luck. – user391339 Jun 21 '14 at 23:00
  • Do you have pip installed? See [this question](http://stackoverflow.com/questions/739993/how-can-i-get-a-list-of-locally-installed-python-modules) – aIKid Jun 21 '14 at 23:15
  • what interpreter are you using in pycharm? – Padraic Cunningham Jun 21 '14 at 23:38
  • the import fails in terminal as well as pycharm. i'm using python 2.7 in both. – user391339 Jun 22 '14 at 00:34
  • aikid: I used pip and got a long list of available modules via pip.get_installed_distributions. In this list I found pycairo==1.8.8. I tried both import cairo and import pycairo to no avail. it's really odd. maybe something specific to the cairo module? interestingly, in the /Library/Python/2.7/site-packages/ path I see 'cairo', but using pip.get_installed_distributions I get pycairo==1.8.8. It doesn't matter if I try importing pycairo or cairo ..... still doesn't work; must be something else. – user391339 Jun 22 '14 at 00:35

1 Answers1

0

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.

  • That's what I get too -- . Still, import cairo directly after this fails with "ImportError: No module named cairo" – user391339 Jun 22 '14 at 00:23
  • Check if cairo exists in /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ – Christopher C. S. Ke Jun 22 '14 at 00:35
  • thanks!! getting closer - you're right, it doesn't exist there. so what does this mean if it is found in /Library/Python/2.7/site-packages/ but not in /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ ? This is the case for PIL the python imaging library, but I can still import PIL successfully. – user391339 Jun 22 '14 at 00:49
  • Perhaps the installation script is not working properly for OS X. A quick solution might be creating symbolic links in your path dir. – Christopher C. S. Ke Jun 22 '14 at 01:01
  • Otherwise you might write sys.path.insert(0, "/Library/Python/2.7/site-packages") before import cairo, but your program will be less portable on other systems. – Christopher C. S. Ke Jun 22 '14 at 01:11
  • I had to set the DYLD_LIBRARY_PATH as /System/Library/Frameworks/Python.framework/Versions/2.7/lib/pkgconfig/ in the environment variables. any idea why that worked? feel free to add this to your answer and I will accept. Thanks for your help. – user391339 Jun 22 '14 at 08:13
  • DYLD_LIBRARY_PATH is not for python. Have you tried the tricks mentioned above? – Christopher C. S. Ke Jun 22 '14 at 08:21
  • I tried all of the above - adding DYLD_LIBRARY_PATH as /System/Library/Frameworks/Python.framework/Versions/2.7/lib/pkgconfig/ to the environment variables in PyCharm was the only thing that worked. – user391339 Jun 23 '14 at 05:39
  • I simply copy cario installed by waf to /System/Library/Frameworks/Python.framework/Versions/2.7/lib, then it works' – Ming Li Nov 23 '14 at 22:01