Search code examples
importmodulespyderpython-3.7

DLL load failed : Python modules accessible through Spyder but import fail in cmd


I have seen many more or less related questions on this topic, but none helped me finding a way to solve my problem. I'm not very familiar with Windows environment, deep apologies if my question sounds obvious to some extent.

Setup:

  • Windows 10
  • Python 3.7.0 (installed through latest Anaconda release)
  • Spyder 3.3.1

Im running a Python, that involves several modules import. I came across some module issues. For example:

from PIL import Image as PIL_Image

Works smooth in Spyder, but gives (error message in french but I guess you got the point):

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Users\my_user_name\Anaconda3\lib\site-packages\PIL\Image.py", line 64, in <module>
    from . import _imaging as core
ImportError: DLL load failed: Le module spécifié est introuvable.

If executed in Powershell or cmd.

What I tried:

  • Appending Anaconda's install path to my path variable before I open Powershell:

    $Env:path += ";C:\Users\my_user_name\Anaconda3;C:\Users\my_user_name\Anaconda3\Scripts"
    
  • Checking that sys.path is the same in Powershell and Spyder. For both, it looks like:

     ['',
      'C:\\Users\\my_user_name',
      'C:\\Users\\my_user_name\\Anaconda3\\python37.zip',
      'C:\\Users\\my_user_name\\Anaconda3\\DLLs',
      'C:\\Users\\my_user_name\\Anaconda3\\lib',
      'C:\\Users\\my_user_name\\Anaconda3',
      'C:\\Users\\my_user_name\\AppData\\Roaming\\Python\\Python37\\site-packages',
      'C:\\Users\\my_user_name\\Anaconda3\\lib\\site-packages',
      'C:\\Users\\my_user_name\\Anaconda3\\lib\\site-packages\\win32',
      'C:\\Users\\my_user_name\\Anaconda3\\lib\\site-packages\\win32\\lib',
      'C:\\Users\\my_user_name\\Anaconda3\\lib\\site-packages\\Pythonwin',
      'C:\\Users\\my_user_name\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
      'C:\\Users\\my_user_name\\.ipython']
    

What else could explain that my import fails outside of Spyder ?


Solution

  • Ok I could not find a clean way out, so let me just share my experience in case someone finds it helpful ...

    The DLL failure with PIL related libs seems to be a known issue. See further explanations:

    How I solved my issue:

    • Cleanly uninstalled anything related to Python on my laptop. Both Anaconda, isolated Python installs, Spyder ...
    • Re-installed the latest Anaconda. It happens to come with Python 3.7.1 instead of 3.7.0
    • Re-did my PIP installs of everything. Note I had to append Anaconda\Library\bin to my Windows path for PIP to work completely, which I don't recall having done with my previous installation.