簡體   English   中英

如何修復 ModuleNotFoundError: 在 Python 中導入 nest 時沒有名為“nest”的模塊?

[英]How to fix ModuleNotFoundError: No module named 'nest' when importing nest in Python?

我已經安裝了 pyNN 和 nest,但我無法導入它。 當我在終端中運行它時,它可以工作,並且程序啟動。 但是,如果我嘗試在 python 中導入:

cd ~
python
import nest

它引發了一個錯誤

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda3/lib/python3.7/site- 
packages/pyNN/nest/__init__.py", line 16, in <module>
    import nest
ModuleNotFoundError: No module named 'nest'

我該如何解決?

編輯:我通過自制軟件(在 macOS Mojave 上)安裝了 pyNN 和 nest。

一般來說,此類問題可以通過檢查 python 版本和site-packages文件夾的位置來解決:

❯ ipython
Python 3.9.0 | packaged by conda-forge | (default, Oct 14 2020, 22:56:29)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pyNN

In [2]: pyNN
Out[2]: <module 'pyNN' from '/usr/local/anaconda3/envs/vision_snn/lib/python3.9/site-packages/pyNN/__init__.py'>

從您的錯誤消息來看,您似乎更喜歡使用 anaconda(就像我一樣,請參閱上面的代碼)。 我建議使用environment.yml文件。 一個最小的例子是:

name: nest
channels:
    - conda-forge
dependencies:
   - nest-simulator
   - pip
   - pip:
     - pyNN

然后,您可以使用以下方法創建虛擬環境:

conda env create -f environment.yml

這對我有用(MacOS 上的 Homebrew):

❯ ipython
Python 3.9.0 | packaged by conda-forge | (default, Oct 14 2020, 22:56:29)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pyNN.nest as sim
[INFO] [2020.11.23 17:6:53 /Users/runner/miniforge3/conda-bld/nest-simulator_1604245451948/work/nestkernel/rng_manager.cpp:217 @ Network::create_rngs_] : Creating default RNGs
[INFO] [2020.11.23 17:6:53 /Users/runner/miniforge3/conda-bld/nest-simulator_1604245451948/work/nestkernel/rng_manager.cpp:260 @ Network::create_grng_] : Creating new default global RNG

              -- N E S T --
  Copyright (C) 2004 The NEST Initiative

 Version: nest-2.20.0
 Built: Nov  1 2020 15:50:27

 This program is provided AS IS and comes with
 NO WARRANTY. See the file LICENSE for details.

 Problems or suggestions?
   Visit https://www.nest-simulator.org

 Type 'nest.help()' to find out more about NEST.

CSAConnector: libneurosim support not available in NEST.
Falling back on PyNN's default CSAConnector.
Please re-compile NEST using --with-libneurosim=PATH
/usr/local/anaconda3/envs/vision_snn/lib/python3.9/site-packages/pyNN/nest/__init__.py:53: UserWarning:Unable to install NEST extensions. Certain models may not be available.

如果您需要更多詳細信息,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM