Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_imaging Failed to Import in from PIL import Image #5573

Closed
johnsunxu opened this issue Jul 2, 2021 · 36 comments
Closed

_imaging Failed to Import in from PIL import Image #5573

johnsunxu opened this issue Jul 2, 2021 · 36 comments
Labels
Installation Usually a problem with … Windows

Comments

@johnsunxu
Copy link

johnsunxu commented Jul 2, 2021

What did you do?

from PIL import Image

What did you expect to happen?

Image would import without errors

What actually happened?

from PIL import Image
  File "C:\...\PIL\Image.py", line 114, in <module>
    from . import _imaging as core
ImportError: DLL load failed while importing _imaging: The specified module could not be found.`

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.9.5 (and 3.9.6)
  • Pillow: 8.3

I installed PIL 8.3 today and then ran into the _imaging import issue. There were previous old threads on a similar problem which mentioned reinstalling pillow but that did not work. I tried importing Image from a different device with python 3.9.5 and Pillow 8.3 without any issues so I then reinstalled python 3.9.5 on the device where pillow did not work. After reinstalling python, Pillow still ran into the same importing problems.

To get pillow to work I had to pip install version 8.2

@radarhere radarhere added Windows Installation Usually a problem with … labels Jul 2, 2021
@cgohlke
Copy link
Contributor

cgohlke commented Jul 2, 2021

Pillow 8.3.0 wheels for Windows are built with libtiff 4.3.0, which has a new dependency (LERC) that uses C++ code and requires the C++ runtime library (MSVCP140.DLL). I think we need to package that DLL in the wheels.

@hugovk
Copy link
Member

hugovk commented Jul 2, 2021

I think we'll probably need an 8.3.1 patch release for the NumPy issue #5571.

@cgohlke To package that DLL in the wheels, are changes needed in this repo, or just in the way you build wheels? Any licensing issues to be aware of?

@cgohlke
Copy link
Contributor

cgohlke commented Jul 2, 2021

To package that DLL in the wheels, are changes needed in this repo

No. Copying the msvc runtime DLLs from the Visual Studio redist directory to the PIL directory before building the wheel seems enough.

Any licensing issues to be aware of?

No. Those DLLs are redistributable.

@KushalSM
Copy link

KushalSM commented Jul 3, 2021

Is there any work-around for this, or fix?
I really need to test my program for my school project

@radarhere
Copy link
Member

@KushalSM I suggest just using Pillow 8.2 instead.

If you're using pip, the command would be pip install Pillow==8.2

@KushalSM
Copy link

KushalSM commented Jul 3, 2021

@KushalSM I suggest just using Pillow 8.2 instead.

If you're using pip, the command would be pip install Pillow==8.2

Ok thank you very much

@cgohlke
Copy link
Contributor

cgohlke commented Jul 3, 2021

Is there any work-around for this, or fix?

Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 vc_redist.x64.exe (64-bit) and vc_redist.x86.exe (32-bit).

Many Python extension packages depend on having the redistributable installed: matplotlib, Pandas, scikit-image...

@dmail00
Copy link

dmail00 commented Jul 3, 2021

Is there any work-around for this, or fix?

Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 vc_redist.x64.exe (64-bit) and vc_redist.x86.exe (32-bit).

Many Python extension packages depend on having the redistributable installed: matplotlib, Pandas, scikit-image...

I have many redistributables installed which include 2015-2019.
I have tried forcing a reinstall, uninstalling and reinstalling and uninstalling Pillow and reinstalling versions 8.2. 8.1 and 8.0 without any success.

Python 3.9.1
OS Windows 10 Pro Version 20H2 OS Build 19042.1052
Pillow 8.3, 8.2 8.1 8.0

@cgohlke
Copy link
Contributor

cgohlke commented Jul 3, 2021

I have many redistributables installed which include 2015-2019.
I have tried forcing a reinstall, uninstalling and reinstalling and uninstalling Pillow and reinstalling versions 8.2. 8.1 and 8.0 without any success.

That is a different issue. The original issue is solved by installing the missing C++ runtime.

@dmail00
Copy link

dmail00 commented Jul 3, 2021

I have many redistributables installed which include 2015-2019.
I have tried forcing a reinstall, uninstalling and reinstalling and uninstalling Pillow and reinstalling versions 8.2. 8.1 and 8.0 without any success.

That is a different issue. The original issue is solved by installing the missing C++ runtime.

This reminded me that last week I installed an older version of Visual Studio (2017) which was required for some work. So, before I created a new issue here, I tried rerunning the vc_redist to see if a repair would fix the issue. Turns out it did. Thanks for the nudge to do so, I can't say for sure that it was because of VS 2017 but there certainly was an issue with the runtime.

@AndrewPardoe
Copy link

Just a +1: installing the VC redist (https://support.microsoft.com/en-us/topic/the-latest-supported-visual-c-downloads-2647da03-1eea-4433-9aff-95f26a218cc0) fixed this for me. I couldn't figure out why it worked on my build machine and not my server.

@radarhere
Copy link
Member

To those still having this issue, could you try again with Pillow 8.3.1?

@hhslepicka
Copy link

@radarhere I have the same issue with Pillow 8.3.1.
I tested it with Python 3.9 and 3.8 on Windows 10.
The error message with Pillow 8.3.1 is:

ImportError: DLL load failed while importing _imaging: %1 not a valid Win32 application.

@hhslepicka
Copy link

@radarhere I can confirm that with Pillow 8.3.1 and installing vc_redist.x64.exe the problem goes away.

@cgohlke
Copy link
Contributor

cgohlke commented Jul 7, 2021

ImportError: DLL load failed while importing _imaging: %1 not a valid Win32 application.

Ahhhh. Looks like the 64-bit wheel build packaged the 32-bit runtime DLLs for whatever reason.

@hhslepicka
Copy link

@cgohlke it looks very much like it.

@radarhere radarhere added this to the 8.4.0 milestone Jul 8, 2021
@cgohlke
Copy link
Contributor

cgohlke commented Jul 8, 2021

@radarhere Do you have any experience with uploading fixed wheels with an added build tag, e.g. Pillow-8.3.1-1-cp38-cp38-win_amd64.whl?

@radarhere
Copy link
Member

I don't, but I'm game to give it a try, as long as there's no objection from @hugovk

@hugovk
Copy link
Member

hugovk commented Jul 9, 2021

No objection!

@cgohlke
Copy link
Contributor

cgohlke commented Jul 11, 2021

The existing 64-bit wheels can be fixed and repackaged with a build number, e.g.:

call py -3.9-64 -m wheel unpack Pillow-8.3.1-cp39-cp39-win_amd64.whl
copy /Y /B "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Redist\MSVC\14.16.27012\x64\Microsoft.VC141.CRT\*.dll" Pillow-8.3.1\PIL\
call py -3.9-64 -m wheel pack --build-number 1 Pillow-8.3.1

To verify that this fixes the issue, install Python and the wheel in a Windows 10 Sandbox (unlike development and CI systems, the Sandbox doesn't have the C++ runtime installed). E.g.:

def test_pillow_wheel(
    wheel='Pillow-8.3.1-1-cp39-cp39-win_amd64.whl',
    pythonurl='https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe',
    test='_test_pillow_wheel',
):
    """Test a local Pillow wheel in a Windows 10 Sandbox.

    1. write Python, Command, and Windows 10 Sandbox scripts to the current
       directory.
    2. run the Windows sandbox script, which runs the command script, which
       downloads and installs Python, pip installs the wheel, and runs the
       Python script.
    3. on successful execution of the scripts, display an image containing
       Pillow version information in Microsoft Paint.
    4. open the log output in notepad.

    """
    import os

    pythonexe = pythonurl.rsplit('/', 1)[-1]
    testdir = rf'C:\Users\WDAGUtilityAccount\Desktop\{test}'
    logfile = f'{testdir}\{test}.txt'

    with open(test + '.wsb', 'w') as fh:
        fh.write(
            rf"""<!-- {test}.wsb -->
<Configuration>
  <MappedFolders>
    <MappedFolder>
      <HostFolder>{os.path.dirname(__file__)}</HostFolder>
      <SandboxFolder>{testdir}</SandboxFolder>
      <ReadOnly>false</ReadOnly>
    </MappedFolder>
  </MappedFolders>
  <LogonCommand>
    <Command>{testdir}\{test}.cmd</Command>
  </LogonCommand>
</Configuration>"""
        )

    with open(test + '.cmd', 'w') as fh:
        fh.write(
            rf""":: {test}.cmd
curl {pythonurl} --output {testdir}\{pythonexe} > {logfile} 2>&1
{testdir}\{pythonexe} /passive Include_tcltk=0 Include_test=0 Include_dev=0 ^
>> {logfile} 2>&1
assoc .png=Paint.Picture
py -m pip install {testdir}\{wheel} >> {logfile} 2>&1
py {testdir}\{test}.py >> {logfile} 2>&1
start {logfile}
        """
        )

    with open(test + '.py', 'w') as fh:
        fh.write(
            f"""# {test}.py
import sys
from PIL import Image, ImageDraw

im = Image.new('L', (512, 64))
dr = ImageDraw.Draw(im)
dr.text(
    (0, 0), text=sys.version + '\\nPillow ' + Image.__version__, fill='white'
)
im.show()
    """
        )

    os.system(test + '.wsb')


test_pillow_wheel()

The fixed 64-bit wheels are here.

@radarhere
Copy link
Member

@cgohlke would you like me to upload those to PyPI now, or do you feel like someone here should check them first?

@radarhere
Copy link
Member

Anyone still having this issue interested in trying one of those wheels? - https://www.lfd.uci.edu/~gohlke/download/Pillow-8.3.1-1-win-dist.zip

@cgohlke
Copy link
Contributor

cgohlke commented Jul 11, 2021

would you like me to upload those to PyPI now

Yes, please upload the new wheels to PyPI. I tested them locally in a Sandbox with the provided script.

@radarhere
Copy link
Member

Ok, done.

Anyone with this issue should now be able to re-install from PyPI to resolve the matter.

@radarhere
Copy link
Member

@johnsunxu does re-installing from PyPI resolve this for you?

@radarhere radarhere removed this from the 8.4.0 milestone Jul 22, 2021
@piaobuliao
Copy link

pip install Pillow-8.3.1
solve the problem

“install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019” Can't solve the problem

@PCG1983
Copy link

PCG1983 commented Apr 23, 2023

Look, my OS is Windows 10 and my Python is 3 and Pillow is 9.5.0, and i'm getting the import error called "cannot import _imaging", u got any way to fix it? plz reply if u do

@radarhere
Copy link
Member

Could you try uninstalling Pillow, check that you can no longer import PIL, and then re-install Pillow with the following?

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

@PCG1983
Copy link

PCG1983 commented Apr 27, 2023

Hey! think i actually fixed it! THX!

@rra88
Copy link

rra88 commented May 31, 2023

Hi, I've run into the same issue with
Python 3.9.16 | packaged by conda-forge | (feeb267e, Jan 18 2023, 16:20:57)
[PyPy 7.3.11 with MSC v.1929 64 bit (AMD64)] on win32

when running
from PIL import Image
which results in
ImportError: cannot import name '_imaging' from 'PIL' (path-to-my-environment\lib\site-packages\PIL_init_.py)
My PIL version is 9.4.0.
Does anyone know what might be the reason in this setup?

@radarhere
Copy link
Member

We don't actually create the wheels distributed on conda. You may want to talk to /~https://github.com/conda-forge/pillow-feedstock instead.

@rra88
Copy link

rra88 commented May 31, 2023

Alright, thanks @radarhere

@jerz4re
Copy link

jerz4re commented Nov 18, 2023

Hey, i've tried a lot of things from different websites including this one and it is still not working any updates ?

@radarhere
Copy link
Member

What is exact command you used to install Pillow?

@jerz4re
Copy link

jerz4re commented Nov 19, 2023

pip install pillow it says its version 10.1.0

@aclark4life
Copy link
Member

@jerz4re This particular issue "bad windows wheels" was fixed 2 years ago. Whether or not you are having that issue is TBD. If you are, try this. Most important, try uninstalling Pillow and make sure you can't import PIL before you do anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Installation Usually a problem with … Windows
Projects
None yet
Development

No branches or pull requests