Python 2与Python 3的导入
问题描述:
我有一个用Python编写的脚本。作者决定使用仅在Python 3中可用的新功能,因此我必须更新我的版本。Python 2与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不是标准库;安装它的Python 2,但该安装不是(并不能)使用被Python 3.
明确地在Python 3安装PIL(或更确切地说,枕头叉),以及:
python3 -m ensurepip # optional, makes sure pip is installed
python3 -m pip install Pillow
PIL不是标准库,您需要安装它。我建议枕头叉(更新PIL包装和错误修复)。 –
单独安装Python 2和Python 3的模块。 Python 3不能“共享”你已经安装在Python 2下的模块。如果你开始使用Python 3,你需要安装任何你想使用的库的Python 3版本。 – BrenBarn
您是否在您的Python 3安装上安装了枕头? –