Import Image using Pillow : No module named 'PIL'

梦想的初衷 提交于 2020-01-07 03:17:06

问题


Several posts were advising to import Pillow using pip, after having uninstalled both PIL and Pillow, what i did :

python -m pip uninstall Pillow (worked)
python -m pip uninstall PIL (PIL was not installed)
python -m pip install Pillow (worked, i guess it was fine already)

Then, according to these posts, using "from PIL import Image" in python should work. I get the error "ImportError: No module named 'PIL'".

I tried "import Image" and "from Pillow import Image" but none of that works either. I'm on windows and using python 3.4.1.

Do you know what to do? Thanks

EDIT : pip had installed Pillow in anaconda3, and not in the python file I use. I copied/pasted the pillow egg file in site-packages and import PIL now works. However, from PIL import Image still does not work : i get ImportError: cannot import name 'Image'

EDIT : the problem was that the egg file was not working proerly (i think). I had to add my own python path to the paths in the environment variables, then i could install pillow with pip. But i now have another error.. from PIL import Image returns :

C:\Users\Loic\Documents\Python\pyzo2014a\lib\site-packages\PIL\Image.py in <module>()

     25 #

     26 

---> 27 from . import VERSION, PILLOW_VERSION, _plugins

     28 

     29 import logging

ImportError: cannot import name 'VERSION'

There is, indeed, no VERSION.py file in the PIL lib. Does that mean i've not installed it correctly? After having defined the python path and installed it with pip in cmd, everything should be fine...


回答1:


I'm on Linux so maybe it doesn't pertain to you, but for me python is Python2.7 and I have to use python3 to get Python3.5

jcomeau@aspire:/usr/src/myturnb$ pip3 install --user Pillow
Collecting Pillow
  Downloading Pillow-4.1.0-cp35-cp35m-manylinux1_i686.whl (5.5MB)
    100% |████████████████████████████████| 5.5MB 55kB/s 
Collecting olefile (from Pillow)
Installing collected packages: olefile, Pillow
Successfully installed Pillow-4.1.0 olefile-0.44
jcomeau@aspire:/usr/src/myturnb$ python3
Python 3.5.2 (default, Jul  5 2016, 11:33:36) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>>

VERSION is defined in __init__.py:

jcomeau@aspire:/usr/src/myturnb$ grep -r VERSION /home/jcomeau/.local/lib/python3.5/site-packages/PIL/ | grep -v '^Binary'
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/__init__.py:VERSION = '1.1.7'  # PIL version
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/__init__.py:PILLOW_VERSION = '4.1.0'  # Pillow
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/__init__.py:__version__ = PILLOW_VERSION
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/Image.py:from . import VERSION, PILLOW_VERSION, _plugins
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/Image.py:    if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/ImageCms.py:VERSION = "1.0.0 pil"
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/ImageCms.py:        VERSION, core.littlecms_version,
/home/jcomeau/.local/lib/python3.5/site-packages/PIL/ImageCms.py:        sys.version.split()[0], Image.VERSION


来源:https://stackoverflow.com/questions/43560898/import-image-using-pillow-no-module-named-pil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!