64

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!

David B
  • 61
  • 8
Nodame
  • 1,053
  • 3
  • 12
  • 18

23 Answers23

110

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

Nodame
  • 1,053
  • 3
  • 12
  • 18
  • 4
    Thank you. This was very helpful. – Prashanth Feb 01 '19 at 20:05
  • Did you have to activate these paths also in some way? – fixingstuff Jun 16 '19 at 21:23
  • 1
    @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. – Nodame Jun 17 '19 at 01:50
  • 7
    For me, it worked by adding just the following two to the PATH: ...\Scripts\ and ...\Library\bin\ – Noopur Gupta Jul 08 '19 at 17:21
  • I also had same issue, i had to install latest pycharm IDE to make it work. – Modem Rakesh goud Jul 09 '19 at 13:52
  • 8
    Apart from `...\Anaconda3` you need only `...\Anaconda3\Library\bin`. For additional functionality such as `pylint`, you can add `...\Anaconda3\Scripts` – Alaa M. Dec 14 '19 at 10:16
  • @Nodame Adding the first PATH was the one that worked for me. Thanks for bringing an end to my multi-hour headache. – Bar-Tzur Jan 21 '20 at 18:54
  • 1
    It works for me!! These paths need to be updated at both the environments i.e System and User. – Satyajit Dhawale Apr 08 '20 at 23:10
  • 2
    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 – CloudWindMoonSun Oct 07 '20 at 20:52
  • 1
    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. – tu_curious May 12 '21 at 19:29
  • 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 – Abdelrahman Shoman Jul 08 '21 at 07:07
  • `Anaconda3\Library\bin` is the only path that's required according to Conda docs at https://docs.conda.io/projects/conda-build/en/latest/resources/use-shared-libraries.html – S Anand Feb 17 '22 at 09:25
  • 2023 and the solution is still working. Was facing the same issue with conda and Tensorflow. – Shtefan Mar 18 '23 at 10:14
  • what happen if i use a custom enviroment? – Hoang Minh Quang FX15045 Apr 10 '23 at 04:26
15

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
Srijan Chaudhary
  • 637
  • 1
  • 8
  • 18
12

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.

DanielTuzes
  • 2,494
  • 24
  • 40
  • For me, only needed to install numpy in (base) to solve my issue. – Greg7000 Jun 08 '21 at 16:43
  • 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? – DanielTuzes Jun 08 '21 at 22:09
5

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

IronFarm
  • 407
  • 4
  • 5
5

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.

heaven2sai
  • 119
  • 2
  • 1
  • 1
    The best answer in my opinion. – MojiProg Dec 13 '19 at 16:27
  • 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 May 12 '21 at 19:25
3

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

Piero Macaluso
  • 1,166
  • 1
  • 10
  • 24
2

Just try uninstall and Reinstalling

pip uninstall numpy

and

pip install numpy 

or pip3 install numpy

sOumyaz D
  • 49
  • 1
  • 3
2

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\
Igor
  • 29
  • 3
2

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.

2

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

smarie
  • 4,568
  • 24
  • 39
2

All I had to do was to force reinstall numpy.

A regular uninstall (pip uninstall numpy) would have removed all the high level packages dependent on Numpy.

I used pip install --upgrade --force-reinstall numpy for reinstalling Numpy forcefully.

Chandan
  • 430
  • 6
  • 9
1

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
lvjiujin
  • 501
  • 4
  • 5
1

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.

1

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

ask4jubad
  • 23
  • 6
1

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.

0

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

Yashwanth
  • 69
  • 7
0

play the below GIF and follow as given.

source - JetBrain

It worked for me..

0

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.

wfgeo
  • 2,716
  • 4
  • 30
  • 51
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aaron Doyle
  • 23
  • 1
  • 6
0

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"}
}
0

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.

Kotodid
  • 879
  • 6
  • 18
0

For Anaconda 3 on windows: use something to search for the Anaconda3 folder.

To use the cmd tool: Change to the root of a drive: (Say C:) like cd /d c: and search like so

C:>dir Anaconda3\Library\bin /AD /s /b

repeat for each drive until you get a hit.

D:>dir Anaconda3\Library\bin /AD /s /b

D:\Anaconda3\Library\bin\1033

knowing this add D:\Anaconda3\Library\bin to the PATH user variable
(we dont want the 1033)

Then restarted the cmd shell. It should now operate

tensorboard --logdir results

TensorBoard 1.14.0 at http://here-x:6006/ (Press CTRL+C to quit)

twobob
  • 354
  • 8
  • 22
-2

Uninstalling and reinstalling numpy worked for me.

pip uninstall numpy

pip install numpy