即使pip说它已安装,也无法在python 2.7中导入模块

时间:2016-12-12 21:33:38

标签: python-2.7 pip python-imaging-library pillow

我已经阅读了关于使用Pillow或Image而不是PIL的很多帖子,但我仍然遇到这个模块的问题。

我在10.9.5上的MBP上运行。

我使用pip来安装图像1.5.5和Pillow 3.4.2。但是,当我在我的脚本中导入模块时,它一直说不存在该名称的模块。

first.last@localhost:/usr/local/bin> pip freeze | grep Pillow
Pillow==3.4.2
first.last@localhost:/usr/local/bin> pip freeze | grep image
image==1.5.5
first.last@localhost:/usr/local/bin> which pip
/usr/local/bin/pip
first.last@localhost:/usr/local/bin> which python
/usr/local/bin/python

Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named image
>>> import PIL
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> import Pillow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Pillow
>>> import pillow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pillow

我检查了/Library/Python/2.7/site-packages,看起来似乎没有.py文件用于枕头或图像,即使pip冻结显示图像和Pillow都已安装。

这是pip的内容就是这个。不确定它是否提供了为什么pip认为模块安装但python说无法找到它的任何线索。

#!/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
    # EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
    __requires__ = 'pip==9.0.1'
    import sys
    from pkg_resources import load_entry_point

    if __name__ == '__main__':
        sys.exit(
            load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
        )

根据我在设置/环境中显示的内容,有人能指出我的方向来开始解决这个问题吗?是否需要更多的其他信息来调试它?

1 个答案:

答案 0 :(得分:1)

由于您在virtualenv之外工作,因此应启用用户站点。使用以下命令查找图像和/或枕头的安装位置:

pip show -f image | grep Location
pip show -f pillow | grep Location

您可能希望看到的内容如下:

Location: /Users/<your_username>/.local/lib/python2.7/site-packages

如果没有安装,请使用--user标志再次卸载并重新安装。

然后使用以下命令确保在您的路径中找到上述结果:

python -m site

您希望在输出中看到的内容类似于

sys.path = [
   ...
]
USER_BASE: '/Users/<your_username>/.local' (exists)
USER_SITE: '/Users/<your_username>/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True

如果安装的位置在您的用户站点中,并且您的用户站点已启用,则导入将起作用。