使用 Jupyter Notebook 的问题

Troubles using JupyterNotebook

我在使用 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,所以我真的不知道我应该再尝试什么了。

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

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