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

83,891

Solution 1

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 😊

Solution 2

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

Solution 3

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.

Solution 4

Get the os environment path like:

import os
os.environ['PATH']

Now, add this path to the environment variable PATH in pycharm debugger/console output. It works.

Solution 5

For PyCharm users wanting an easy fix, this issue is fixed in PyCharm 2019.2.

Share:
83,891
Nodame
Author by

Nodame

Updated on July 22, 2022

Comments

  • Nodame
    Nodame almost 2 years

    I am using the Anaconda distribution with Python 3.7. Among the packages installed, I have numpy, pandas, etc. In PyCharm IDE, I have set the Project Interpreter to be the path to the python.exe installed with Anaconda: C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\python.exe

    However, when I try running a simple python script:

    import numpy as np
    print(np.pi)
    

    I get an error:

    ImportError: 
    Importing the multiarray numpy extension module failed.  Most
    likely you are trying to import a failed build of numpy.
    If you're working with a numpy git repo, try `git clean -xdf` (removes all
    files not under version control).  Otherwise reinstall numpy.
    
    Original error was: DLL load failed: The specified module could not be found.
    
    See the full error message below:
    
    ----------------------------------------------------------------------------------
    Traceback (most recent call last):
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
        from . import multiarray
    ImportError: DLL load failed: The specified module could not be found.
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:/Users/my_user_name/Documents/TestMyApps/simpletest.py", line 1, in <module>
        import numpy as np
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
        from . import add_newdocs
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
        from numpy.lib import add_newdoc
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
        from .type_check import *
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
        import numpy.core.numeric as _nx
      File "C:\Users\my_user_name\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
        raise ImportError(msg)
    ImportError: 
    Importing the multiarray numpy extension module failed.  Most
    likely you are trying to import a failed build of numpy.
    If you're working with a numpy git repo, try `git clean -xdf` (removes all
    files not under version control).  Otherwise reinstall numpy.
    
    Original error was: DLL load failed: The specified module could not be found.
    
    
    Process finished with exit code 1
    
    ----------------------------------------------------------------------------------
    

    I have tried uninstalling and reinstalling numpy using Anaconda Prompt with: conda uninstall numpy, and then: conda install numpy [I now have numpy 1.15.4]. The re-install seem 'successful' (at least according to Anaconda Prompt), but, I keep getting an error in PyCharm.

    The strange part, is when I directly go into Anaconda Prompt, and type:

    python
    import numpy as np
    print(np.pi)
    

    I do not get any error, and I am able to see the correct result printed out. At first, I thought that perhaps, when using PyCharm, I am somehow pointing to a different executable, but I checked in both Anaconda Prompt and Python Console of Pycharm, using:

    sys.executable
    

    and they both show the same path:

    'C:\\Users\\my_user_name\\AppData\\Local\\Continuum\\anaconda3\\python.exe'
    

    Note that in PyCharm, when I try running a script where I am not importing any library, or when the library imported are just like sys or os, the scripts run fine. However, when I try running any other script that involves importing a library that somehow depends on numpy, it fails as well (i.e. scripts where I import pandas, etc.) Those same scripts work fine in Anaconda Prompt.

    I am at a loss here, any help would be very appreciated!

  • Prashanth
    Prashanth about 5 years
    Thank you. This was very helpful.
  • fixingstuff
    fixingstuff almost 5 years
    Did you have to activate these paths also in some way?
  • Nodame
    Nodame almost 5 years
    @fixingstuff , I did not have to activate these paths in any way. AlI had to do was to add the paths to the System Environment Variable PATH.
  • Noopur Gupta
    Noopur Gupta almost 5 years
    For me, it worked by adding just the following two to the PATH: ...\Scripts\ and ...\Library\bin\
  • Modem Rakesh goud
    Modem Rakesh goud almost 5 years
    I also had same issue, i had to install latest pycharm IDE to make it work.
  • MojiProg
    MojiProg over 4 years
    The best answer in my opinion.
  • Alaa M.
    Alaa M. over 4 years
    Apart from ...\Anaconda3 you need only ...\Anaconda3\Library\bin. For additional functionality such as pylint, you can add ...\Anaconda3\Scripts
  • Bar-Tzur
    Bar-Tzur over 4 years
    @Nodame Adding the first PATH was the one that worked for me. Thanks for bringing an end to my multi-hour headache.
  • CS QGB
    CS QGB over 4 years
    useless for me(:
  • Satyajit Dhawale
    Satyajit Dhawale about 4 years
    It works for me!! These paths need to be updated at both the environments i.e System and User.
  • CloudWindMoonSun
    CloudWindMoonSun over 3 years
    Thank you very much. It worked for me. However I used these paths: C:\ProgramData\Anaconda3, C:\ProgramData\Anaconda3\Library, C:\ProgramData\Anaconda3\Library\bin, C:\ProgramData\Anaconda3\Library\mingw-w64\bin, and C:\ProgramData\Anaconda3\Scripts
  • tu_curious
    tu_curious almost 3 years
    Definitely the best answer. Takes care of different possible paths on different platforms. Just to elaborate, use the above python command through your working conda environment in command line (Anaconda prompt, Terminal, etc.). Then add the output of the command to your System path. Restart VS code and it should work.
  • tu_curious
    tu_curious almost 3 years
    Very useful thanks! But, these paths have been changing on different platforms and with updated/different versions of Anaconda/Miniconda. The answer by @heaven2sai is agnostic to such changes and therefore better IMO.
  • Greg7000
    Greg7000 almost 3 years
    For me, only needed to install numpy in (base) to solve my issue.
  • DanielTuzes
    DanielTuzes almost 3 years
    Conda is often installed with admin rights and you, the user doesn't have admin rights to modify the base. It is a common practice in corporate environments. Have you tried to execute the debugger in another environment?
  • Abdelrahman Shoman
    Abdelrahman Shoman almost 3 years
    I added the following 3 path to the system PATH but it did not work ..\Anaconda3\envs\myenv, ..\Anaconda3\envs\myenv\Library\bin, ..\Anaconda3\envs\myenv\Scripts. I then followed what @SatyajitDhawale said by adding them to User PATH and it worked fine. I am not sure if User PATH was enough or did I have to add them to both PATH variables
  • Tranbi
    Tranbi over 2 years
    This answer has already been proposed stackoverflow.com/a/68162635/13525512
  • brethvoice
    brethvoice over 2 years
    Please update for new version of Anaconda
  • brethvoice
    brethvoice over 2 years
    I am using Pycharm 2021.X and it is still a problem for me.
  • S Anand
    S Anand about 2 years
    Anaconda3\Library\bin is the only path that's required according to Conda docs at docs.conda.io/projects/conda-build/en/latest/resources/…