简体   繁体   English

如何在jupyter笔记本中指定使用哪个python和哪个模块?

[英]How do I specify which python and which modules are being used in my jupyter notebook?

When I do 当我做

import sys
sys.executable

I get '/usr/local/opt/python/bin/python2.7' in my ordinary python shell and '/usr/bin/python' in IPython or my jupyter notebook. 我在普通的python shell中得到了'/usr/local/opt/python/bin/python2.7' ,在IPython或jupyter笔记本中得到了'/usr/local/opt/python/bin/python2.7' '/usr/bin/python' I would like to force my jupyter notebook to use this same python that the shell is using. 我想强制我的jupyter笔记本使用外壳程序使用的同一Python。 I have installed many modules and would like to be able to use the same ones in jupyter than I am using already in the shell. 我已经安装了许多模块,并且希望能够在jupyter中使用与我已经在shell中使用的模块相同的模块。 How can I do this? 我怎样才能做到这一点?

The simplest way is to install IPython and Jupyter with the Python you want them to use. 最简单的方法是使用您希望它们使用的Python安装IPython和Jupyter。 You can do this using pip: 您可以使用pip执行此操作:

path/to/python -m pip install jupyter

You could alternatively set up the IPython kernel to run with your desired Python without reinstalling the notebook. 您可以选择将IPython内核设置为与所需的Python一起运行,而无需重新安装笔记本。 See the docs on installing kernels . 请参阅有关安装内核的文档 This is more complicated than just installing everything again, though. 但是,这比再次安装所有内容要复杂得多。

I had the same problem when using jupyter from a virtualenv. 使用来自virtualenv的jupyter时,我遇到了同样的问题。

In my case I had two kernels named python3 . 就我而言,我有两个名为python3的内核。 Doing a jupyter kernelspec list it reported only one kernel named python3 that pointed to an incorrect binary. 做一份jupyter kernelspec list它只报告了一个名为python3的 内核 ,该内核指向错误的二进制文件。 I removed it using jupyter kernelpec remove python3 and magically appeared the correct one pointing to my activated virtualenv. 我使用jupyter kernelpec remove python3删除了它,并神奇地出现了指向我激活的virtualenv的正确代码。

If you need to reinstall the kernel then, from the bin directory of the virtualenv you can do, 如果您需要重新安装内核,则可以 virtualenv 的bin目录中执行操作,

./python -m pip install ipykernel sudo ./python -m ipykernel install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道 Jupyter notebook 中运行的是哪个 Python? - How to know which Python is running in Jupyter notebook? jupyter notebook 如何导入不在当前工作目录中的模块? - How does jupyter notebook import modules which are not in the current working directory? 我怎么知道我的 jupyter notebook 正在使用哪个 python 以及如何更改为使用 conda python? - How can I know that which python my jupyter notebook is using and how can I change to use conda python? 如何在我的jupyter笔记本中添加python3内核? - How do I add a python3 kernel to my jupyter notebook? 如何检查哪个 Jupyter notebook 已经“%run”了另一个 notebook? - How do I check which Jupyter notebook has “%run” another notebook? Matplotlib:我怎么知道正在使用哪个颜色图? - Matplotlib: how do I know which colormap is being used? 我将python模块放入哪个目录? - Which directory do I put python modules in? 如何选择要运行的Jupyter笔记本? - How to choose which jupyter notebook to run? 如何让用 ImageMagick 制作的 animation 在我的 python Jupyter Notebook 上播放? - How do I make animation made with ImageMagick play on my python Jupyter Notebook? 我如何使用 jupyter 笔记本的 python 代码从网站下载 csv 文件 - how do i download the csv file from a website using python code for my jupyter notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM