python framework is detected,成功安装python模块(cairo)但无法导入python时,可能是什么问题?...

本文讲述了如何处理Python模块安装后在PyCharm IDE中导入失败的问题,涉及检查目录、环境变量设置、包管理器确认及路径配置,通过实例分析了cairo图形模块的安装与导入问题,提供了解决步骤和实用技巧。
摘要由CSDN通过智能技术生成

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.

193165d7a0592552abb843f77bd7bca8.png

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.

e7248937f08dddf2d52de6176277126a.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值