简体   繁体   English

Python 2与Python 3导入

[英]Python 2 vs Python 3 imports

I have a script that is written in Python. 我有一个用Python编写的脚本。 The author decided to use new features only available in Python 3, so I had to update my version. 作者决定使用仅在Python 3中可用的新功能,因此我必须更新我的版本。

Now I am having trouble because the script crashes on an import statement, so I decided to do some debugging. 现在我遇到了麻烦,因为脚本在导入语句时崩溃,所以我决定进行一些调试。 I came to the conclusion that my Python 3 cannot import Image from PIL . 我得出的结论是我的Python 3无法从PIL导入Image

In Python 2: 在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

Does not give an error, but in Python 3: 没有给出错误,但是在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'

Why is this happening and how can I fix this? 为什么会发生这种情况,我该如何解决?

PIL is not a standard library; PIL不是标准库; you installed it for Python 2, but that installation is not (and cannot be) used by Python 3. 您是为Python 2安装的,但Python 3没有(也不能)使用该安装。

Install PIL (or rather, the Pillow fork) explicitly in Python 3 as well: 还要在Python 3中显式安装PIL(或更确切地说是Pillow分支):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM