6

I'm trying to run a Python script from the Windows shell, but when I do

python path\to\file\script.py

I get an error "DLL load failed: The specified module could not be found" and it traces back to the line where I import numpy.

C:\Users\Admin>python path\to\file\script.py
Traceback (most recent call last):
  File "path\to\file\script.py", line 8, in <module>
    import numpy as np
  File "C:\Users\Admin\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\Admin\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.

The weird part is that when I run it in an editor like Spyder, numpy imports just fine. Can someone help me?

Thanks

Jack Hanson
  • 336
  • 1
  • 2
  • 11

3 Answers3

3

It's fixed Anaconda path issue. Check your %PATH% if it's correctly defined.

Source: https://github.com/numpy/numpy/issues/12957

Valijon
  • 12,667
  • 4
  • 34
  • 67
  • It worked! I also had to update conda. But if I want several libraries in my script, do I have to add them all to my path? Ex if I want pandas do I have to make another PATH variable? – Jack Hanson Sep 23 '19 at 18:23
  • You need to include them all, cuz conda and python shares %PATH% variable. Also you may use other PATH variable – Valijon Sep 23 '19 at 18:36
2

This is a common problem when installing python packages, mainly in windows.

Numpy and other packages have bindings to C++ code that require some special dependencies.

Rather than trying to get the dependencies exactly right for compiling the package, you can use a precompiled "wheel" file from one of several sources.

I use Chris Gholke's site

download the .whl file and install using

pip install directory/path/package.whl

edit: and as a note, the python environment you access from powershell or cmd is different from the anaconda environment in spyder. One of the differences between conda and pip is that conda installed precompiled packages while pip does not.

Hugh_Kelley
  • 988
  • 1
  • 9
  • 23
  • The answer from @Valijon worked for me, and I think it is preferable because it stays within the scope of Anaconda and I don't need to use pip install. I will keep this in mind, though, in case I have a project where I'm not using Anaconda – Jack Hanson Sep 23 '19 at 18:30
  • @JackHanson, no problem thanks for the upvote. When you say "windows shell" in your original question, did you mean the anaconda shell, windows command line, windows powershell? – Hugh_Kelley Sep 23 '19 at 21:06
  • The command line. Is it Would it have been more appropriate to say command line in the question title? – Jack Hanson Sep 23 '19 at 23:11
  • maybe but people can always just ask, the only reason it matters is that you can end up in different environments when accessing python through different ways, https://realpython.com/python-virtual-environments-a-primer/ – Hugh_Kelley Sep 24 '19 at 16:07
0

I solved my issues with Numpy DLL load issues by replacing Anaconda3 with WinPython.

Timo Riikonen
  • 477
  • 5
  • 8