numpy is already installed with Anaconda but I get an ImportError (DLL load failed: The specified module could not be found)

I have actually figured out the solution. I had initially edited my System Environment Variable ‘PATH’ by adding the path to the Scripts folder in my Python executable directory. Turns out, this was NOT enough...

I had to add more paths to it, and that magically did the trick.

See below the paths I added for the System Environment Variable PATH:

C:\Users\<myusername>\AppData\Local\Continuum\Anaconda3\Scripts\
C:\Users\<myusername>\AppData\Local\Continuum\Anaconda3\Library\
C:\Users\<myusername>\AppData\Local\Continuum\Anaconda3\Library\bin\
C:\Users\<myusername>\AppData\Local\Continuum\Anaconda3\Library\mingw-w64\bin\

Now, I am not sure exactly which one path among these 4 my System was really looking for in particular. However, adding this combination sure did work for me 😊


Edit your System Environment Variable "PATH’ (refer steps here) by adding below given path.

Note: Follow this step if you already have numpy installed.

If using anaconda:
C:\Users\<username>\AppData\Local\Continuum\Anaconda<version>\Library\bin\

If using independent python interpreter:
C:\Users\<username>\AppData\Roaming\Python\Python<version>\Library\bin

In my case, conda didn't place the packages where my IDE (VS Code) or pytest expected them. Although I was able to call pytest within the environment, VS Code was unable [1] to debug the pytest modules. I came up with 2 solutions.

  • Solution A: Start VS Code from an Activated environment
    1. make sure you have numpy installed into a conda env, e.g. to base
    2. start an activated command line, e.g. Anaconda Prompt or Anaconda Powershell Prompt
    3. start your IDE from the activated command line (e.g. by issuing code to open VS Code)
    4. you can use numpy package while debugging pytest
  • Solution B: install numpy with pip
    1. if you have numpy installed by conda, remove it first
    2. install numpy with pip
    3. now you can open your IDE directly, no need to open it from an activated environment

A thread on VS Code's python extension tells that this is an issue on conda's side.

[1]: I was able to run pytest manually, from an activated environment, but pytest, started from VS Code's debugger somehow was not aware of numpy's location. The error message I've got after starting a debug session on a pytest function was the following:

_______________________ ERROR collecting test_pytest.py _______________________
ImportError while importing test module 'c:\Users\userFolder\test_pytest.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\miniconda3\lib\site-packages\numpy\core\__init__.py:22: in <module>
    from . import multiarray
..\..\miniconda3\lib\site-packages\numpy\core\multiarray.py:12: in <module>
    from . import overrides
..\..\miniconda3\lib\site-packages\numpy\core\overrides.py:7: in <module>
    from numpy.core._multiarray_umath import (
E   ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:
..\..\miniconda3\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_pytest.py:2: in <module>
    import numpy
..\..\miniconda3\lib\site-packages\numpy\__init__.py:140: in <module>
    from . import core
..\..\miniconda3\lib\site-packages\numpy\core\__init__.py:48: in <module>
    raise ImportError(msg)
E   ImportError: 
E   
E   IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
E   
E   Importing the numpy C-extensions failed. This error can happen for
E   many reasons, often due to issues with your setup or how NumPy was
E   installed.
E   
E   We have compiled some common reasons and troubleshooting tips at:
E   
E       https://numpy.org/devdocs/user/troubleshooting-importerror.html
E   
E   Please note and check the following:
E   
E     * The Python version is: Python3.8 from "C:\Users\userFolder\miniconda3\python.exe"
E     * The NumPy version is: "1.19.2"
E   
E   and make sure that they are the versions you expect.
E   Please carefully study the documentation linked above for further help.
E   
E   Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.