简体   繁体   中英

Windows VS Code can't find numpy Module even though it is installed (ModuleNotFoundError: No module named 'numpy.core._multiarray_umath')

I have been working with Anaconda and VS Code on my Windows and everything was fine until my computer once crashed. After that, I couldn't run my scripts that worked perfectly fine before.

For example, I now get the the error when running:

import os
import datetime

import IPython
import IPython.display
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from pmdarima import auto_arima
from kerastuner import HyperModel, Objective
from kerastuner import BayesianOptimization
import keras.backend as K
from tensorflow.keras.layers import (
    Dense,
    Dropout,
    LSTM,
    GRU
)


mpl.rcParams['figure.figsize'] = (8, 6)
mpl.rcParams['axes.grid'] = False

C:\Users\myname\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\overrides.py in <module>
      6 
----> 7 from numpy.core._multiarray_umath import (
      8     add_docstring, implement_array_function, _get_implementing_args)

ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-1-aaaeab3b7b86> in <module>
      4 import IPython
      5 import IPython.display
----> 6 import matplotlib as mpl
      7 import matplotlib.pyplot as plt
      8 import numpy as np

~\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py in <module>
    105 # cbook must import matplotlib only within function
    106 # definitions, so it is safe to import from it here.
--> 107 from . import cbook, rcsetup
    108 from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence
    109 from matplotlib.cbook import mplDeprecation  # deprecated

~\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\cbook\__init__.py in <module>
     26 import weakref
     27 
---> 28 import numpy as np
     29 
     30 import matplotlib

~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\__init__.py in <module>
    138     from . import _distributor_init
    139 
--> 140     from . import core
    141     from .core import *
    142     from . import compat

~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\__init__.py in <module>
     46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
     47         __version__, exc)
---> 48     raise ImportError(msg)
     49 finally:
     50     for envkey in env_added:

ImportError: 

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

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "C:\Users\myname\AppData\Local\Programs\Python\Python39\python.exe"
  * The NumPy version is: "1.19.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'

I have been reading multiple posts but couldn't find a solution. I tried multiple things:

  • I have uninstalled and reinstalled anconda

  • I have added C:\Users\myname\AppData\Local\Programs\Python\Python39 to my system variable path

  • use pip uninstall numpy and after that conda install numpy

  • set my python path in VS code to either C:\Users\myname\AppData\Local\Programs\Python\Python39 (pythn verison 3.9.4) or C:\Users\myname\anaconda3 (Python version 3.8.5)

  • I went to the link mentioned in the error message but couldn't find a solution

  • tried to upgarde my numpy version (I have 1.20.2)

    (pip install numpy --upgrade Requirement already up-to-date: numpy in c:\users\myname\anaconda3\lib\site-packages (1.20.2))

  • Python Verison: 3.8.5 and 3.9.4 (tried both)

  • Numpy Version: 1.20.1

  • Matplotlib Version: 3.4.1

This

import os
print(sys.version)
print("PYTHONPATH:", os.environ.get('PYTHONPATH'))
print("PATH:", os.environ.get('PATH'))

leads to this output:

3.9.4 (tags/v3.9.4:1f2e308, Apr  6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]
PYTHONPATH: c:\Users\myname\.vscode\extensions\ms-toolsai.jupyter-2021.5.745244803\pythonFiles;c:\Users\myname\.vscode\extensions\ms-toolsai.jupyter-2021.5.745244803\pythonFiles\lib\python
PATH: C:\Users\myname\anaconda3;C:\Users\myname\anaconda3\Library\mingw-w64\bin;C:\Users\myname\anaconda3\Library\usr\bin;C:\Users\myanme\anaconda3\Library\bin;C:\Users\myname\anaconda3\Scripts;C:\Users\myname\anaconda3\bin;C:\Users\myname\anaconda3\condabin;C:\Users\myname\anaconda3;C:\Users\myname\anaconda3\Library\mingw-w64\bin;C:\Users\myname\anaconda3\Library\usr\bin;C:\Users\myname\anaconda3\Library\bin;C:\Users\myname\anaconda3\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\MATLAB\R2020a\bin;C:\Users\myname\AppData\Local\Programs\Python\Python39;C:\Users\myname\AppData\Local\Microsoft\WindowsApps;C:\Users\myname\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\myname\AppData\Local\Programs\MiKTeX\miktex\bin\x64;C:\Users\myname\AppData\Local\Programs\Git\cmd;C:\Program Files (x86)\GitHub CLI;C:\Users\myname\AppData\Local\GitHubDesktop\bin

Thanks in advance for any help!

You have a lot going on here, including multiple installations of Python. It looks like you have both an Anaconda installation of Python, and a standard Python installation as well.

My first recommendation is to use virtual Python environments. These create isolated environments where you can install packages without worrying about messing up your base installation. There are a handful of ways to do this, Anaconda comes with Conda, and Python comes with venv. Both work well. I prefer Conda.

Step 1 - one or more of the packages in your standard Python installation are corrupted or incompatible; I would recommend uninstalling completely.

-- Windows Key > Settings (Gear icon) > Apps > Click on Python > Uninstall

Step 2 - assuming your Anaconda installation is not also corrupted, we can use Conda to create a new environment.

-- Windows Key > "Anaconda Prompt" > this opens a terminal with the base Anaconda environment.

conda create -n myenv python tensorflow ipython matplotlib pandas seaborn numpy -y

Step 3 - activate the environment to use it and install PyPi packages.

conda activate myenv
pip install pmdarima

Fells like magic, but after applying the steps mentioned by @James, restarting my laptop, disabling and reenabling all extensions in VS Code as well as setting the environment to myenv in Anaconda itself before launching VS Code, it finally works now. Don't know what exactly made the difference though. Thanks again @James for the help! 在启动 VS Code 之前将 Anaconda 本身的环境更改为 myenv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM