Rami Ma
Rami Ma

Reputation: 1025

Importing the numpy c-extensions failed

Importing the numpy c-extensions failed

I installed python 3.7 on my windows system to work on visual studio code. Everything was going well, including using the libraries. I uninstalled python using the uninstalling program tool in control panel. And installed Miniconda 3. I checked that everything works well, and then installed the numpy library using conda install numpy in my terminal GitBash on windows 10, then checked it on my visial studio code, but it failed to start.

Reproducing code example:

import numpy as np
A = np.array([[-1], [7], [-26]])

Error message:

Traceback (most recent call last): File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core__init__.py", line 17, in from . import multiarray File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in from . import overrides File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core\overrides.py", line 7, in from numpy.core._multiarray_umath import ( ImportError: DLL load failed: Не найден указанный модуль.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:/Users/ramim/Desktop/22/Matrix library/alsf.py", line 3, in import numpy as np File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy__init__.py", line 142, in from . import core File "C:\Users\ramim\Miniconda3\lib\site-packages\numpy\core__init__.py", line 47, in raise ImportError(msg) ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed. - Try uninstalling and reinstalling numpy. - If you have already done that, then: 1. Check that you expected to use Python3.7 from "C:\Users\ramim\Miniconda3\python.exe", and that you have no directories in your PATH or PYTHONPATH that can interfere with the Python and numpy version "1.17.3" you're trying to use. 2. If (1) looks fine, you can open a new issue at https://github.com/numpy/numpy/issues. Please include details on: - how you installed Python - how you installed numpy - your operating system - whether or not you have multiple versions of Python installed - if you built from source, your compiler versions and ideally a build log

  • If you're working with a numpy git repository, try git clean -xdf (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on an existing issue about this - open a new one instead.

Original error was: DLL load failed: Не найден указанный модуль.

Numpy/Python version information:

Python 3.7.5 Numpy 1.17.3

I tried to uninstall and install the numpy library again, but it was useless. Note: when I type in the teminal conda install numpy, it says:

All requested packages already installed

That's how I checked if numpy is really installed!

How to solve that?

Upvotes: 77

Views: 167046

Answers (19)

Jon
Jon

Reputation: 1152

The problem with the other answers here is that they want you to essentially upgrade some version of your packages (i.e. pip install --upgrade numpy will upgrade your numpy to the latest)... but what if you have a hard requirement to stay at the current version you are already at?

Some of the answers will work, but only if you separately install the numpy dependencies after the rest of your dependencies. If you are attempting to set this up from scratch (i.e with python3 -m pip install -r requirements.txt in a new virtual environment), you will run into this error until you also run python3 -m pip install numpy==<the older version of numpy you need>, after having initially installed your dependencies. Which essentially installs some dependencies twice or needlessly separates them into two commands, and is not ideal.

When this happens, the root issue is that some package in the pip cache causes the incorrect mixture of C compile libs... and that will keep happening on every new install, until you clear the pip cache...

If you run python3 -m pip install --no-cache-dir -r requirements.txt (with your numpy defined in the requirements.txt), and that works, but it does not after removing the --no-cache-dir option, then the cache dir is the problem.

Simply run python3 -m pip cache purge to clear the wheel packages, then re-run python3 -m pip install -r requirements.txt. Now the packages will install correctly on the first try every time.

Upvotes: 1

MasterOne Piece
MasterOne Piece

Reputation: 461

I solved it by: pip uninstall <module_name>
For example: pip uninstall numpy , You need to do it twice.
Then install it: pip install numpy
The last error is in Matplotlib, do as before for numpy.
Remember to uninstall twice because the module is installed using pip and conda.
This is how I solved my problem after 2 hours googling and binging.

Upvotes: 0

Cato Minor
Cato Minor

Reputation: 3099

I hit this error while running a block of code in a .ipynb Jupyter Notebook file.

I fixed the problem by switching the version of Python that was running. Specifically, I was running Python 3.8.5 from the Anaconda3 directory on my computer and I switched this to Python 3.9.2 that I had installed elsewhere on my computer.

To toggle the version of Python in VSCode while working on a .ipynb file, click on the version specified in the top-right corner of the screen. Thereafter, a dropdown menu will allow you to select from different versions of Python installed on your computer.

Upvotes: 0

Nasira Mujawar
Nasira Mujawar

Reputation: 11

Please check both of these (your PATH or PYTHONPATH environment variables) carefully to see if they are what you expect.

In my case, there was an issue related to updated versions of python and NumPy, I resolved both problems by running a single command over conda PowerShell prompt i.e. conda update --all

Here are the steps-

  1. Open Anaconda Powershell Prompt and click 'run as Administrator'.
  2. Enter the following code into the prompt and click enter: conda update --all ...

You are all set to run your script

Upvotes: 1

Siddharth Sharma
Siddharth Sharma

Reputation: 1711

Uninstalled anaconda and installed latest version
I tried the answers here but they didn't work for me, uninstalled older version of anaconda (don't remember previous version number now) and installed Anaconda3-2022.05 (Anaconda Navigator 2.1.4). This sorted it for me. I hadn't used my anaconda setup in long time and thus didn't care if my preinstalled packages get wiped out. This took a lot less time and saved from pain of trying more approaches.
OS: Windows 10

Upvotes: 0

Hans Bouwmeester
Hans Bouwmeester

Reputation: 1529

As I'm on MacOS (with an M1 CPU) I realize my answer is not exactly an answer to the poster's question but as I got the same error and Google sent me here perhaps this helps someone.

In my case I found the error was caused because VSCode was still running in Intel-X86 mode on my Mac-M1. Simply re-installing VSCode using the latest "Mac Universal" distribution switched it to ARM64 mode fixing the issue for me.

Upvotes: 0

Alex Delarge
Alex Delarge

Reputation: 29

I had the same problem, and tried several of the solutions given here, but none worked for me. I looked in another forum and was able to solve the problem (https://github.com/numpy/numpy/issues/13252):

conda uninstall numpy

pip3 install numpy.

Upvotes: 1

Sam
Sam

Reputation: 1618

For my situation, I had updated the version of python that I was using. Running git clean -xdf resolved the issue.

My system:

  • OS: Mac OS 11.6
  • Python: 3.7.8 => 3.8.12
  • Numpy: 1.20.2
  • Poetry: 1.1.6

Upvotes: -1

Rikki
Rikki

Reputation: 3538

For those scratching their head wondering why re-installing numpy module works:

If you are using a virtual environment (say in a relative location called env), make sure that folder does not make it to other platforms like Docker, or different OS you might be switching to.

Remember the compilation of Python and C extensions necessary for numpy are platform dependent. So if it works in Windows, it will not work in Linux using the same virtual environment (cached modules).

For example, this is particularly important if you are running your code on Windows - and then try to run it on a Linux container via Docker Desktop. (Make sure env - or whatever your virtual env is called - is ignored and not copied onto the container)

Upvotes: 1

Star
Star

Reputation: 93

I solved the problem by reinstalling NumPy through pip:

pip install --upgrade --force-reinstall numpy
                 

Upvotes: 5

lalbadelmondo
lalbadelmondo

Reputation: 229

Similar with my problem recently. I'm using Python 3.8 by Miniconda on Win 10 system. I solved the problem by changing default terminal from PowerShell to Command Prompt.

Steps:

  1. Open VS Code's Command Palette menu by pressing Ctrl+Shift+P or F1
  2. Choose "Terminal: Select Default Profile" entry
  3. Then pick "Command Prompt" option
  4. Restart VS Code

Upvotes: 22

J B
J B

Reputation: 440

In my case, I had to manually 'conda activate myenv' the desired environment in the VS code terminal. Previously, I only had to select the python version from the desired environment, and then the environment would auto-activate. This answer references and confirms Brett Cannon's comment above, which was the sole reason I thought to try it.

Upvotes: 0

Anggi BK
Anggi BK

Reputation: 1

This issue is still ongoing. I use VS code with conda venv, and solved it in a similar way with marineCoder:

  • In addition to numpy and pandas, I also remove matplotlib using conda uninstall
  • Cautiously reinstall pandas and numpy using pip install

In my case, the error shows up whenever matplotlib package is installed, so I got to either remove it or downgrade the three of them. There is a clash on numpy dependency. Another related issue is shown in this post:

I get `No module named _multiarray_umath` when using matplotlib

Upvotes: 0

marineCoder
marineCoder

Reputation: 51

I solved this by the following steps-

  1. Uninstalling numpy and pandas with conda
  2. Installing numpy and pandas with pip

Upvotes: 4

thirstygerry
thirstygerry

Reputation: 61

I solved it by removing ALL numpy versions

pip uninstall numpy
pip3 uninstall numpy

And then installing numpy and libatlas-base-dev via apt-get

sudo apt-get remove python3-numpy
sudo apt-get install libatlas-base-dev

Upvotes: 3

Anne
Anne

Reputation: 593

Not sure this is a thing in Visual Studio too, but for Eclipse I had to change one of the environmental variables.

Setup: Windows, Python 3.7, Conda venv with numpy

Solution:

CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1

For Eclipse, the environment variables can be accessed via Properties -> Run/Debug Settings -> Edit -> Environment.

Anaconda also documented the fix, albeit for a different problem: Conda Troubleshooting

Upvotes: 25

algerjiang
algerjiang

Reputation: 121

This issue is caused by VScode default terminal (powershell) setting, To switch VScode default terminal from powershell to cmd, the conda env will be activated correctly, other powershell will try to invoke conda activate xxxxxx which will fail, then the subsequent import numpy will fail.

So two ways to fix it:

  1. Fix path search issue under powershell environment to get conda activate xxxxxx successfully executed;

  2. Change vs code default terminal to 'cmd': add "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe" in settings.json

Upvotes: 12

Igor
Igor

Reputation: 1737

It looks like there's something wrong with your %PATH%. It might either contain some conflicts, or just be too long (>2047 chars). Try adding the folder with the dlls (from the environment you're trying to use) to the very beginning of it:

PATH=C:\Users\ramim\Miniconda3\Library\bin;%PATH%
python -c "import numpy"

(based on this)

Upvotes: 1

user4043890
user4043890

Reputation:

Try to uninstall numpy and setuptools first:

  1. pip uninstall -y numpy

  2. pip uninstall -y setuptools

  3. pip install setuptools

  4. pip install numpy

Borrowed from solution provided by mehdiHadji here- https://github.com/ipython/ipyparallel/issues/349

Upvotes: 63

Related Questions