简体   繁体   中英

Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image'

When I run the following in the Spyder IDE on Win10:

import PIL
import os

Inboundfile = "C:/Test/test/Overlay.png"

image = PIL.Image.open(os.path.abspath(Inboundfile))
image.show()  

It works fine. Opens the picture and outputs:

runfile('C:/Users/bob/Anaconda3/envs/NewEnvironment/bob/test3.py', wdir='C:/Users/bob/Anaconda3/envs/NewEnvironment/bob')

But when I call the same script from a cmd prompt:

C:\Users\bob\Anaconda3\envs\NewEnvironment\bob>python test3.py

It results in:

Traceback (most recent call last):
  File "test3.py", line 14, in <module>
    image = PIL.Image.open(os.path.abspath(Inboundfile))
AttributeError: module 'PIL' has no attribute 'Image'

I have verified that the environment path is pointing to the same python environment as the IDE. ( I think ) Here is my Path variable from OS

C:\Users\bob\Anaconda3\envs\NewEnvironment\  

It's probably something very simple but I'm very new at this and any help would be appreciated.

I tried adding the line:

from PIL import Image

It still runs fine in the IDE but the error message changes to:

C:\Users\bb\Anaconda3\envs\NewEnvironment\bob>python test3.py
Traceback (most recent call last):
  File "test3.py", line 11, in <module>
    from PIL import Image
  File "C:\Users\bob\Anaconda3\envs\NewEnvironment\lib\site-packages\PIL\Image.py", line 69, in <module>
    from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.

This is even more confusing as I can follow that path and see Image.py sitting in the directory. What is the IDE doing that CMD is not?

All of my problems vanished when I simply closed and restarted the console I was using to call the python script. I have learned that the console saves things like the path variables when it starts up and must be restarted to get changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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