首页 > 解决方案 > 如何将包导入 conda 环境?

问题描述

我在 conda 中创建了一个虚拟环境。conda activate然后我在 anaconda 提示符 ( )上打开了环境。然后,我安装了我打算在那个环境中使用的任何包(例如,比如说 numpy)。

当我打开 jupyter notebook 并运行import numpy时,这显示了错误

ImportError                               Traceback (most recent call last)
~\anaconda3\envs\new1\lib\site-packages\numpy\core\__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

~\anaconda3\envs\new1\lib\site-packages\numpy\core\multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

~\anaconda3\envs\new1\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)

ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-5-d4cdadb62aa7> in <module>
----> 1 import numpy

~\anaconda3\envs\new1\lib\site-packages\numpy\__init__.py in <module>
    143     from . import _distributor_init
    144 
--> 145     from . import core
    146     from .core import *
    147     from . import compat

~\anaconda3\envs\new1\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.7 from "C:\Users\<my user>\anaconda3\envs\new1\python.exe"
  * The NumPy version is: "1.20.1"

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

Original error was: DLL load failed: The specified module could not be found.

我试过跑步

python
>>>import numpy

在 anaconda 提示符中,它确实成功导入了它。即使在 jupyter notebook 中,当我运行help("modules")numpy 时,它也会显示为列出的模块之一。

预期的行为是应该导入已安装的模块。

另外,如果我遗漏了什么,请告诉我,这是我在 stackoverflow 上的第一篇文章。

标签: pythonjupyter-notebookanacondaconda

解决方案


通常您会打开 anaconda 导航器,然后转到左侧的环境,然后在底部单击“添加”以添加新环境。之后,您单击新创建的环境和“打开终端”。在您使用的那个终端中:conda install -c anaconda numpy. 现在您应该可以直接从 anaconda 导航器打开您的 jupyter notebook,也可以通过终端启动它,一切正常。

如果它仍然不起作用,请尝试重新启动 anaconda,它不应该被要求,但有时它对我有用。


推荐阅读