【问题标题】:Troubles using JupyterNotebook使用 Jupyter Notebook 的问题
【发布时间】:2024-01-20 18:09:02
【问题描述】:

我在使用 JupyterNotebook 时遇到了问题。

当我尝试在使用 JupyterNotebook 时导入 SciPy 和 NumPy 时,我总是遇到错误:

import scipy as sp 
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-6b0df46be2d8> in <module>
----> 1 import scipy as sp

~\Anaconda3\envs\MachineL\lib\site-packages\scipy\__init__.py in <module>
     60 __all__ = ['test']
     61 
---> 62 from numpy import show_config as show_numpy_config
     63 if show_numpy_config is None:
     64     raise ImportError(

ImportError: cannot import name 'show_config'

当我尝试检查 NumPy 是否有效时:

import numpy as np
print("I like", np.pi)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-dbcfc58336e0> in <module>
      1 import numpy as np
----> 2 print("I like", np.pi)

AttributeError: module 'numpy' has no attribute 'pi'

但真正令人困惑的是,当我在终端中尝试所有操作时,一切正常。

>>> import scipy as sp
>>> print(sp.__version__)
1.3.1

>>> import numpy as np
>>> print("I like", np.pi)
I like 3.141592653589793

我不知道问题可能是什么...... 我首先认为我的 JupyterNotebook 会使用不同的 VirtualEnvironment,所以我尝试了这个:

(在 JupyterNotebook 中):

pip list
Package              Version  
-------------------- ---------
absl-py              0.8.1    
astor                0.8.0    
attrs                19.2.0   
backcall             0.1.0    
bleach               3.1.0    
certifi              2019.9.11
colorama             0.4.1    
cycler               0.10.0   
decorator            4.4.0    
defusedxml           0.6.0    
entrypoints          0.3      
gast                 0.2.2    
google-pasta         0.1.7    
grpcio               1.24.1   
h5py                 2.10.0   
ipykernel            5.1.2    
ipython              7.8.0    
ipython-genutils     0.2.0    
jedi                 0.15.1   
Jinja2               2.10.3   
jsonschema           3.0.2    
jupyter-client       5.3.4    
jupyter-core         4.6.0    
Keras-Applications   1.0.8    
Keras-Preprocessing  1.1.0    
kiwisolver           1.1.0    
Markdown             3.1.1    
MarkupSafe           1.1.1    
matplotlib           3.1.1    
mistune              0.8.4    
nbconvert            5.6.0    
nbformat             4.4.0    
notebook             6.0.1    
numpy                1.17.3   
opt-einsum           3.1.0    
pandas               0.25.1   
pandocfilters        1.4.2    
parso                0.5.1    
pickleshare          0.7.5    
pip                  19.2.3   
prometheus-client    0.7.1    
prompt-toolkit       2.0.10   
protobuf             3.10.0   
Pygments             2.4.2    
pyparsing            2.4.2    
pyrsistent           0.15.4   
python-dateutil      2.8.0    
pytz                 2019.3   
pywin32              223      
pywinpty             0.5.5    
pyzmq                18.1.0   
scipy                1.3.1    
Send2Trash           1.5.0    
setuptools           41.4.0   
six                  1.12.0   
tensorboard          2.0.0    
tensorflow           2.0.0    
tensorflow-estimator 2.0.0    
termcolor            1.1.0    
terminado            0.8.2    
testpath             0.4.2    
tornado              6.0.3    
traitlets            4.3.3    
wcwidth              0.1.7    
webencodings         0.5.1    
Werkzeug             0.16.0   
wheel                0.33.6   
wincertstore         0.2      
wrapt                1.11.2

然后我在我的 VirtualEnvironment 中尝试了同样的事情:

Package              Version  
-------------------- ---------
absl-py              0.8.1    
astor                0.8.0    
attrs                19.2.0   
backcall             0.1.0    
bleach               3.1.0    
certifi              2019.9.11
colorama             0.4.1    
cycler               0.10.0   
decorator            4.4.0    
defusedxml           0.6.0    
entrypoints          0.3      
gast                 0.2.2    
google-pasta         0.1.7    
grpcio               1.24.1   
h5py                 2.10.0   
ipykernel            5.1.2    
ipython              7.8.0    
ipython-genutils     0.2.0    
jedi                 0.15.1   
Jinja2               2.10.3   
jsonschema           3.0.2    
jupyter-client       5.3.4    
jupyter-core         4.6.0    
Keras-Applications   1.0.8    
Keras-Preprocessing  1.1.0    
kiwisolver           1.1.0    
Markdown             3.1.1    
MarkupSafe           1.1.1    
matplotlib           3.1.1    
mistune              0.8.4    
nbconvert            5.6.0    
nbformat             4.4.0    
notebook             6.0.1    
numpy                1.17.3
opt-einsum           3.1.0    
pandas               0.25.1   
pandocfilters        1.4.2    
parso                0.5.1    
pickleshare          0.7.5    
pip                  19.2.3   
prometheus-client    0.7.1    
prompt-toolkit       2.0.10   
protobuf             3.10.0   
Pygments             2.4.2    
pyparsing            2.4.2    
pyrsistent           0.15.4   
python-dateutil      2.8.0    
pytz                 2019.3   
pywin32              223      
pywinpty             0.5.5    
pyzmq                18.1.0   
scipy                1.3.1    
Send2Trash           1.5.0    
setuptools           41.4.0   
six                  1.12.0   
tensorboard          2.0.0    
tensorflow           2.0.0    
tensorflow-estimator 2.0.0    
termcolor            1.1.0    
terminado            0.8.2    
testpath             0.4.2    
tornado              6.0.3    
traitlets            4.3.3    
wcwidth              0.1.7    
webencodings         0.5.1    
Werkzeug             0.16.0   
wheel                0.33.6   
wincertstore         0.2      
wrapt                1.11.2

我使用的python版本:

import sys
print(sys.version)

3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 14:00:49) [MSC v.1915 64 bit (AMD64)]

这个输出和我终端的输出一样

在我尝试过之后,我很确定两者都使用相同的 VirtualEnvironment,所以我真的不知道我应该再尝试什么了。

感谢您的宝贵时间,希望有人能帮助我。

【问题讨论】:

  • 你能在 Jupyter notebooks 的新模块上尝试 !pip install numpy 然后再次比较库列表以验证 CMD / Jupiter Notebooks 在相同的环境中工作吗?
  • @Ian-Fogelman 我刚试过。在 Jupyter Notebook 中:` !pip install numpy 要求已经满足:c:\users\kampet\anaconda3\envs\machinel\lib\site-packages (1.17.3) 中的 numpy ` 只是为了测试,我也在终端中尝试过: ` (MachineL) C:\Users\Kampet>pip install numpy 要求已经满足:c:\users\kampet\anaconda3\envs\machinel\ lib\site-packages (1.17.3) 中的 numpy ` 我希望这是什么你的意思是。
  • 为什么我不能格式化我的代码大声笑.. 对不起,我对 * 很陌生。顺便回复一下。

标签: python numpy scipy jupyter-notebook importerror


【解决方案1】:

在你的 Jupiter notebook 目录中的任何地方可能有一个 numpy.py 脚本吗?这可能优先于实际的 numpy 模块。尝试更改您使用 jupiter notebook 的目录并重新启动内核,我相信这将解决问题。

【讨论】:

  • 啊,谢谢老兄...我已经读到了,但我总是看错目录。我的笨蛋将一些 python 文件命名为“numpy.py”...“scipy.py”,我当时想:“是的,我稍后会在我再次处理它们时重命名它们。”但这导致了整个问题。谢谢你的那个人