问题
I'm using a Mac OS x 10.10.3 Yosemite, and Python 2.7.9 |Anaconda 2.2.0 (x86_64) for a lot of python stuff. I'm using eclipse, and google app engine.
I'm running out of stack overflow posts to read for this error that a lot of people have, then resolve by some means that has not work for me.
I'm getting this error:
import Image ImportError: No module named Image
From this code:
try:
from PIL import Image
except:
import Image
After I already tried the following:
conda install pillow
sudo pip install pillow
pip install pillow
pip install pil
Some stack overflow post suggested seeing if the path was there for another user, so I tried this in my terminal:
python
Python 2.7.9 |Anaconda 2.2.0 (x86_64)| (default, Dec 15 2014, 10:37:34)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import sys
>>> print(sys.path)
and got:
['',
'/Users/jrussek/anaconda/lib/python27.zip',
'/Users/jrussek/anaconda/lib/python2.7',
'/Users/jrussek/anaconda/lib/python2.7/plat-darwin',
'/Users/jrussek/anaconda/lib/python2.7/plat-mac',
'/Users/jrussek/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/jrussek/anaconda/lib/python2.7/lib-tk',
'/Users/jrussek/anaconda/lib/python2.7/lib-old',
'/Users/jrussek/anaconda/lib/python2.7/lib-dynload',
'/Users/jrussek/anaconda/lib/python2.7/site-packages',
'/Users/jrussek/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3 py2.7.egg',
'/Users/jrussek/anaconda/lib/python2.7/site-packages/aeosa',
'/Users/jrussek/anaconda/lib/python2.7/site-packages/cryptography-0.8-py2.7-macosx-10.5-x86_64.egg',
'/Users/jrussek/anaconda/lib/python2.7/site-packages/setuptools-14.3-py2.7.egg']
I thought it wasn't in my path so I tried to append the thing:
PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL
didn't look like it showed up.
So I tried uninstalling, and reinstalling, in 10 different ways. I only did that for pillow, not PIL because I tried different things for PIL and looked at a lot of posts that said use pillow, not PIL because PIL is deprecated.
Anyway, for pillow I keep getting:
Requirement already up-to-date: pillow in ./anaconda/lib/python2.7/site-packages
clearly pillow is already installed in anaconda, but that doesn't help the import error I keep getting.
so then I tried conda install pillow,
and it upgraded some stuff:
but.. I try running my code again, and I still get no module found, no matter what import statement I try.
Suggestions? Please? Anyone?
回答1:
The following does work for me:
from PIL import Image
And this does not work:
import Image
Pillow package
$ conda search pillow
Fetching package metadata: ....
pillow 2.1.0 py33_0 defaults
2.1.0 py27_0 defaults
2.1.0 py26_0 defaults
2.3.1 py34_0 defaults
2.3.1 py33_0 defaults
2.3.1 py27_0 defaults
2.3.1 py26_0 defaults
2.4.0 py34_0 defaults
2.4.0 py33_0 defaults
2.4.0 py27_0 defaults
2.4.0 py26_0 defaults
. 2.5.1 py34_0 defaults
2.5.1 py33_0 defaults
2.5.1 py27_0 defaults
2.5.1 py26_0 defaults
2.7.0 py34_0 defaults
2.7.0 py33_0 defaults
2.7.0 py27_0 defaults
2.7.0 py26_0 defaults
2.7.0 py34_1 defaults
2.7.0 py33_1 defaults
2.7.0 py27_1 defaults
2.7.0 py26_1 defaults
2.8.1 py34_1 defaults
2.8.1 py33_1 defaults
2.8.1 py27_1 defaults
2.8.1 py26_1 defaults
2.8.1 py34_2 defaults
2.8.1 py33_2 defaults
2.8.1 py27_2 defaults
2.8.1 py26_2 defaults
2.8.2 py34_0 defaults
2.8.2 py33_0 defaults
2.8.2 py27_0 defaults
2.8.2 py26_0 defaults
* 2.9.0 py34_0 defaults
2.9.0 py33_0 defaults
2.9.0 py27_0 defaults
2.9.0 py26_0 defaults
Python path
$ python
Python 3.4.3 |Anaconda 2.1.0 (x86_64)| (default, Mar 6 2015, 12:07:41)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print (sys.path)
['', '/Users/erwin/anaconda/lib/python34.zip', '/Users/erwin/anaconda/lib/python3.4', '/Users/erwin/anaconda/lib/python3.4/plat-darwin', '/Users/erwin/anaconda/lib/python3.4/lib-dynload', '/Users/erwin/anaconda/lib/python3.4/site-packages', '/Users/erwin/anaconda/lib/python3.4/site-packages/Sphinx-1.3.1-py3.4.egg', '/Users/erwin/anaconda/lib/python3.4/site-packages/setuptools-18.1-py3.4.egg']
Image usage
This works both in my terminal and in Eclipse PyDev now.
>>> from PIL import Image
>>> a = Image.new("RGB", (512,512), "red")
>>> a.show()
回答2:
At first, try installing Pillow with (Capital letter P)
pip install Pillow
then use from PIL import Image "Pillow is a fork of PIL, the Python Imaging Library, which is no longer maintained. However, to maintain backwards compatibility, the old module name is used." From: pillow installed, but "no module named pillow" - python2.7 - Windows 7 - python -m install pillow
回答3:
Newer version of Pillow has been changed decleration of "image" as:
from PIL.Image import core as image
回答4:
I've been in the same position as you at least a few times, and I somehow managed to resolve it through other solutions. Then it broke again. Here is what I did that fixed it:
Open two finder windows.
In one, navigate to:
//anaconda/pkgs/pillow-3.2.0-py27_0/lib/python2.7/site-packages
In this folder, you will find the folder
PIL
Copy this folder and paste it somewhere accessible.
Now open your python 2.7 environment folder within the anaconda directory. For me, it's:
//anaconda/envs/py27/lib/python2.7/site-packages
You should only have to change the "py27" in that.
Even if there is already a folder in there named "PIL", put your copied PIL in the new site-packages folder. From what I can tell, the existing PIL folder in there is old or wrong, or something. Or pillow doesn't install it correctly. I think this amounts to a manual installation of a python module. Super fun.
回答5:
I was running into the same problem as OP:
- macOS 10.12.5
- Python 2.7.13 [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
The problem ended up being the the .py file I was trying to run.
At the top of the script there was a shebang: #!/usr/bin/python
However, my default python does not run from the system installation. I used homebrew
to upgrade python, so my default path is:
$ which python
/usr/local/bin/python
I changed the shebang at the top to #!/usr/local/bin/python
, and after that I was able to run my script (which was calling from PIL import Image
).
来源:https://stackoverflow.com/questions/31016085/importerror-no-module-named-image-importerror-no-module-named-pil-python