如何在Anaconda Python 3.7.6中正确使用numpy。

11

我正在尝试在Python中使用NumPy。我刚刚安装了Anaconda Python 3.7,看起来一切都很顺利。但是,我无法导入numpy(使用import numpy这一行)。当我这样做时,我会收到以下错误:

    C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py:140: 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
Traceback (most recent call last):
  File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
    from . import multiarray
  File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
    from . import overrides
  File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
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 "<stdin>", line 1, in <module>
  File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Users\jsmith\anaconda3\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
    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\jsmith\anaconda3\python.exe",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.1" 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: The specified module could not be found.

我可以在Anaconda Navigator的环境选项卡中看到它,当我尝试在Eclipse(Pydev)中使用时,它会出现在强制内置模块下。我查看了我的PYTHONPATH,我的Eclipse环境和基本python目录(jsmith/anaconda3)都在其中。我已经尝试导入其他我在强制内置模块下看到的库,它们都能正常工作,但numpy似乎是唯一有问题的。调用pip install numpy告诉我它已经安装了版本1.18.1。我查看了堆栈溢出页面,并在anaconda提示符中运行了答案中的第一个命令(conda create -n test numpy python=3.7 --no-default-packages)。这个方法可行,然后我意识到测试是针对问题特定的,于是尝试使用base,但出现了以下错误:
CondaValueError: The target prefix is the base prefix. Aborting.

然而,调用conda activate base没有任何作用。

6
尝试执行pip uninstall numpy命令,然后执行conda install -n base numpy命令。 - cel
太好了!那确实起作用了,不过我使用的是 pip install numpy 而不是 conda install -n base numpy - John Smith
3个回答

22

正如@cel在评论中提到的那样,使用 pip uninstall numpypip install numpy卸载并重新安装numpy使其正常工作。


0
更好的方法是:
import os
import sys
os.path.dirname(sys.executable)

这将为您提供环境路径。将该路径放入Python设置中。


-1
打开Anaconda Prompt: 然后,您必须进入要在PowerBI中使用的Conda环境。我有一个名为'temp'的环境,所以我首先在'Anaconda Prompt'中激活它。
(base) C:\Users\ashish>conda activate temp

然后我进入安装文件夹中包含“PowerBI”可执行文件的目录:

(temp) C:\Users\ashish>cd "C:\Program Files\Microsoft Power BI Desktop\bin"

然后,我从命令提示符中启动PowerBI:

(temp) C:\Program Files\Microsoft Power BI Desktop\bin>PBIDesktop.exe

这将解决您遇到的NumPy错误。如果您想要使用PowerBI与其他任何软件包,请在相应的“Conda环境”中安装该软件包(在我的情况下是“temp”)。
还要确保Python主目录(Anaconda3)也已添加到Python脚本部分的“Power BI Desktop”全局选项中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接