【问题标题】:Python 2 vs Python 3 importsPython 2 与 Python 3 导入
【发布时间】:2016-02-04 02:12:19
【问题描述】:

我有一个用 Python 编写的脚本。作者决定使用仅在 Python 3 中可用的新功能,因此我不得不更新我的版本。

现在我遇到了麻烦,因为脚本在导入语句上崩溃了,所以我决定进行一些调试。我得出的结论是,我的 Python 3 无法从 PIL 导入 Image

在 Python 2 中:

Python 2.7.10 (default, Aug 22 2015, 20:33:39) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image

不报错,但在 Python 3 中:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'PIL'

为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • PIL 不是标准库,需要安装。我推荐 Pillow fork(更新的 PIL 打包和错误修复)。
  • Python 2 和 Python 3 的模块是分开安装的。 Python 3 无法“共享”您已在 Python 2 下安装的模块。如果您开始使用 Python 3,则需要安装任何要使用的库的 Python 3 版本。
  • 您是否在 Python 3 安装中安装了枕头?

标签: python python-2.7 python-import pillow python-3.5


【解决方案1】:

PIL 不是标准库;您为 Python 2 安装了它,但 Python 3 不(也不能)使用该安装。

在 Python 3 中也显式安装 PIL(或者更确切地说是 Pillow fork):

python3 -m ensurepip  # optional, makes sure pip is installed
python3 -m pip install Pillow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多