gpt4 book ai didi

python - Jupyter 笔记本,错误的 sys.path 和 sys.executable

转载 作者:太空狗 更新时间:2023-10-30 00:58:57 33 4
gpt4 key购买 nike

我正在尝试在 Jupyter Notebook 中运行 python 库的 anaconda 分发,但是当我运行笔记本时,我不断收到 ImportErrors,因为 python 路径设置为 Mac OS X 10.11 的默认分发

当我打印出 sys.path 和 sys.executable 时,它​​们在运行 python 和运行 jupyter notebook 时有所不同。例如,

from pprint import pprint as p
import sys

p(sys.path)

在 python 中执行此操作后,我得到了正确的输出:

['',
'/Users/glennraskovich/anaconda2/lib/python27.zip',
'/Users/glennraskovich/anaconda2/lib/python2.7',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-darwin',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac',
'/Users/glennraskovich/anaconda2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-tk',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-old',
'/Users/glennraskovich/anaconda2/lib/python2.7/lib-dynload',
'/Users/glennraskovich/anaconda2/lib/python2.7/site-packages',
'/Users/glennraskovich/anaconda2/lib/python2.7/site-packages/aeosa']

但是在 jupyter notebook 中运行时我得到:

['',
'/usr/local/lib/python2.7/site-packages/dask-0.11.0-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/networkx-1.11-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/Pillow-3.3.1-py2.7-macosx-10.11-x86_64.egg',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions',
'/Users/glennraskovich/.ipython']

对于 sys.executable,

p(sys.executable)

在 python 中,正确的输出:

/Users/glennraskovich/anaconda2/bin/python

但是在jupyter notebook中,sys.executable没有设置成anaconda版本

/usr/local/opt/python/bin/python2.7

我已经尝试在我的 .bashrc 和 .bash_profile 中设置 PATH,并使用 which python、which jupyter 等命令显示 anaconda 路径,但 jupyter notebook 没有使用 anaconda 路径。这可能是什么问题?

最佳答案

我找到了解决方案,因为内核设置为使用默认的 mac os x 的 python,我使用命令修复了它

python2 -m pip install ipykernel

python2 -m ipykernel 安装--用户

关于python - Jupyter 笔记本,错误的 sys.path 和 sys.executable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634660/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com