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

PythonPython 3.xNumpyPycharm

Python Problem Overview


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!

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

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

Solution 5 - Python

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 6 - Python

I just faced the same issue and found the top solution above useful (https://stackoverflow.com/a/54131905/3645664).

In particular, I added following path at the user profile level and NumPy started working normal:

C:\Anaconda3\Library\bin\

Solution 7 - Python

Adding C:\ProgramData\Anaconda3\envs\tf-gpu\Library\bin to System Path also worked for me.

Solution 8 - Python

Same Solution here as DanielTuzes's solution A, but I write it down here more clearly:

If you are using VS Code + Windows + Anaconda you should :

 - Open the command palette (Control-Shift-P)
 - search for Terminal: Select Default Profile
 - select: Command Prompt

 That did the trick for me !

That's because the PowerShell in VS Code is not integrated by default with anaconda prompt, this latter using CMD and not the PowerShell.

Solution 9 - Python

Just try uninstall and Reinstalling

pip uninstall numpy

and

pip install numpy 

or pip3 install numpy

Solution 10 - Python

Adding Anaconda to System PATH didn't work for me, but uninstalling and reinstalling did.

Solution 11 - Python

I tried to include the PATH but it didn't work. So I tried reinstalling Anaconda and realized that the previous time I installed it, I had not enabled the option "Add Anaconda3 to my PATH environment variable". I enabled that and installed and it worked perfectly.

Solution 12 - Python

I had this issue with numpy 1.20.1 and the following:

  • Visual Studio Code 1.59.0
  • Anaconda 2.0.3
  • Python 3.8.8

I could import numpy using the Anaconda command prompt but not with Visual Studio Code.

I resolved it by uninstalling and then reinstalling numpy then it worked.

Solution 13 - Python

Even I was running the python file on command prompt, but the python.exe was from Anaconda. so when you run any python file you get,

> Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation.

you need to activate the environment by using this command

 conda init cmd.exe
 conda activate 

you see the command window as below, with base env

(base) C:\Users\yash\Documents\pycharm_project\venv>

Doing this I resolved this error

> ImportError: numpy.core.multiarray failed to import Traceback (most recent call last): File "opencv_p1.py", line 2, in import cv2 File "E:\Anaconda3\lib\site-packages\cv2_init_.py", line 3, in from .cv2 import * ImportError: numpy.core.multiarray failed to import

In case you want to deactivate the conda use

 conda deactivate

Please follow this link in case you have any doubt https://conda.io/activation

Solution 14 - Python

The problem is that there are two different versions of numpy. what you can do is uninstall the numpy lib. I think you must uninstall numpy two times. because two different numpy versions.

pip uninstall numpy
pip uninstall numpy

Solution 15 - Python

play the below GIF and follow as given.

source - JetBrain

It worked for me..

Solution 16 - Python

May be a bit of a braindead answer but I had this happen to me in PyCharm because I tried to run my script before it finished "discovering binary modules". And then even after completing this process, I still couldn't run the code without getting this error.

Simply restarting PyCharm fixed it.

Solution 17 - Python

I found my issue was in how VSCode was setup. Even though I was in a virtual environment, it was not running the version I installed. If you are using a virtual enviroment, check the path of the python.exe that it is using to run your code. I.E. C:/Users/user_name/Anaconda3/python.exe vs c:/Users/user_name/Documents/pythonTesting/appName/Scripts/python.exe

Running pip list showed the correct version, but running the script gave an error with a different version.

Eventually I noticed that VSCode was using the default Conda instead of the python.exe that was in my virtual environment.

To fix this you need you need to press CTRL+SHIFT+P (on Windows anyway) and choose interpreter, find your virtual environments Scripts folder, and point it to the python.exe within.

Bingo.

Solution 18 - Python

I encountered the problem in my conda package manager and jupyter notebook on Windows 10 system. Adding CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1 solved the problem.

Two-step solution.

(1) Find where your environment config of ipykernel is:

jupyter kernelspec list

In my case the path was C:\ProgramData\jupyter\kernels\myenv

(2) Add "env": {"CONDA_DLL_SEARCH_MODIFICATION_ENABLE":"1"} in kernel.json file in the path. For instance, this is my kernel.json after editing (don't forget to add comma to preserve correct json format):

{
 "argv": [
  "C:\\Users\\tsund\\miniconda3\\envs\\myenv\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myenv",
 "language": "python",
 "metadata": {
  "debugger": true
 },
 "env": {"CONDA_DLL_SEARCH_MODIFICATION_ENABLE":"1"}
}

Solution 19 - Python

Caught same error, found this thread, but nothing help. I do not have anaconda, but use numpy in windows service using win32serviceutil. NB, problem does not occured in cmd - only into service.

Solution was to install actual VC++ redistributable (!!!) (Microsoft Visual C++ 2015 x64 in my environment). Nothing points on this library.

Hope this answer will help anyone.

Solution 20 - Python

I ran into the problem lately with Pycharm 2021.3.1 .

My solution was simply to remove the interpreter from pycharm's "python interpreters" list, and to re-add it again. I then waited for the skeletons, indexes, etc. to be built by pycharm, and re-run the same run configuration.

Surprisingly, everything was working again !

I had to do it twice in two days though, it does not seem very stable. Apparently PyCharm has some issues in setting the PATH correctly for conda envs.

Maybe it will be fixed soon, I'll try the updates

See https://youtrack.jetbrains.com/issue/PY-31896#focus=Comments-27-5858759.0-0

Solution 21 - Python

Uninstalling and reinstalling numpy worked for me.

pip uninstall numpy

pip install numpy

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNodameView Question on Stackoverflow
Solution 1 - PythonNodameView Answer on Stackoverflow
Solution 2 - PythonSrijan ChaudharyView Answer on Stackoverflow
Solution 3 - PythonDanielTuzesView Answer on Stackoverflow
Solution 4 - PythonIronFarmView Answer on Stackoverflow
Solution 5 - Pythonheaven2saiView Answer on Stackoverflow
Solution 6 - PythonIgorView Answer on Stackoverflow
Solution 7 - PythonChandran PalanisamyView Answer on Stackoverflow
Solution 8 - PythonCharbel-Raphaël SegerieView Answer on Stackoverflow
Solution 9 - PythonsOumyaz DView Answer on Stackoverflow
Solution 10 - Pythonask4jubadView Answer on Stackoverflow
Solution 11 - PythonLeonardo Artiles MonteroView Answer on Stackoverflow
Solution 12 - PythonCodeologistView Answer on Stackoverflow
Solution 13 - PythonYashwanthView Answer on Stackoverflow
Solution 14 - PythonlvjiujinView Answer on Stackoverflow
Solution 15 - PythonGouranga SatapathyView Answer on Stackoverflow
Solution 16 - PythonwfgeoView Answer on Stackoverflow
Solution 17 - PythonAaron DoyleView Answer on Stackoverflow
Solution 18 - PythonMarat ZaynutdinoffView Answer on Stackoverflow
Solution 19 - PythonKotodidView Answer on Stackoverflow
Solution 20 - PythonsmarieView Answer on Stackoverflow
Solution 21 - PythonJosue OrtegaView Answer on Stackoverflow