使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33496350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:27:17  来源:igfitidea点击:

Execute Python script within Jupyter notebook using a specific virtualenv

pythonpython-3.xjupyter-notebookvirtualenvjupyter

提问by Bede Constantinides

I would like to execute a long running Python script from within a Jupyter notebook so that I can hack on the data structures generated mid-run.

我想从 Jupyter 笔记本中执行长时间运行的 Python 脚本,以便我可以破解运行中生成的数据结构。

The script has many dependencies and command line arguments and is executed with a specific virtualenv. Is it possible to interactively run a Python script inside a notebook from a specified virtualenv (different to that of the Jupyter installation)?

该脚本有许多依赖项和命令行参数,并使用特定的 virtualenv 执行。是否可以从指定的 virtualenv(与 Jupyter 安装的不同)以交互方式在笔记本内运行 Python 脚本?

Thanks!

谢谢!

采纳答案by muon

Here's what worked for me (non condapython): (MacOS, brew version of python. if you are working with system python, you may (will) need prepend each command with sudo)

这是对我conda有用的(非python):(MacOS,python 的 brew 版本。如果你使用系统 python,你可能(将)需要在每个命令前加上sudo

first activate virtualenv

首先激活virtualenv

if starting afresh then, e.g., you could use virtualenvwrapper

如果重新开始,例如,您可以使用 virtualenvwrapper

$pip install virtualenvwrapper
$mkvirtualenv -p python2 py2env 
$workon py2env

# This will activate virtualenv

(py2env)$ 

# Then install jupyter within the active virtualenv
(py2env)$ pip install jupyter

# jupyter comes with ipykernel, but somehow you manage to get an error due to ipykernel, then for reference ipykernel package can be installed using:
(py2env)$ pip install ipykernel

Next, set up the kernel

接下来,设置内核

(py2env)$ python -m ipykernel install --user --name py2env --display-name "Python2 (py2env)"

then start jupyter notebook (the venv need not be activated for this step)

然后启动 jupyter notebook(这一步不需要激活 venv)

(py2env)$ jupyter notebook
# or
#$ jupyter notebook

in the jupyter notebook dropdown menu: Kernel >> Change Kernel >> <list of kernels>you should see Python2 (py2env)kernel

在 jupyter notebook 下拉菜单中:Kernel >> Change Kernel >> <list of kernels>您应该看到 Python2 (py2env)内核

This also makes it easy to identify python version of kernel, and maintain either side by side.

这也可以很容易地识别内核的python版本,并并行维护。

here is the link to detail docs http://ipython.readthedocs.io/en/stable/install/kernel_install.html

这是详细文档的链接 http://ipython.readthedocs.io/en/stable/install/kernel_install.html

回答by edhodapp

I found this link to be very useful:

我发现这个链接非常有用:

https://ocefpaf.github.io/python4oceanographers/blog/2014/09/01/ipython_kernel/

https://ocefpaf.github.io/python4oceanographers/blog/2014/09/01/ipython_kernel/

Make sure that you pip install jupyter into your virtualenv. In case the link goes away later, here's the gist:

确保你 pip install jupyter 到你的 virtualenv 中。如果链接稍后消失,这里是要点:

You need to create a new kernel. You specify your kernel with a JSON file. Your kernels are usually located at ~/.ipython/kernels. Create a directory with the name of your virtualenv and create your kernel.json file in it. For instance, one of my paths looks like ~./ipython/kernels/datamanip/kernel.json

您需要创建一个新内核。您可以使用 JSON 文件指定内核。您的内核通常位于~/.ipython/kernels. 创建一个名为 virtualenv 的目录,并在其中创建 kernel.json 文件。例如,我的路径之一看起来像~./ipython/kernels/datamanip/kernel.json

Here's what my kernel.json file looks like:

这是我的 kernel.json 文件的样子:

{
  "display_name": "Data Manipulation (Python2)",
  "language": "python",
  "codemirror_mode": {
    "version": 3,
    "name":"ipython"
  },
  "argv": [
    "/Users/ed/.virtualenvs/datamanip/bin/python",
    "-c",
    "from IPython.kernel.zmq.kernelapp import main; main()",
    "-f",
    "{connection_file}"
    ]
}

I am not certain exactly what the codemirror_mode object is doing, but it doesn't seem to do any harm.

我不确定 codemirror_mode 对象到底在做什么,但它似乎没有任何伤害。

回答by singer

A bit more simple solution to get notebook kernels available in other notebooks.

让笔记本内核在其他笔记本中可用的更简单的解决方案。

I'm using Linux + virtualenv + virtualenvwrapper. If you are using different setup, change some commands to the appropriate ones, but you should get the idea.

我正在使用 Linux + virtualenv + virtualenvwrapper。如果您使用不同的设置,请将一些命令更改为适当的命令,但您应该明白这一点。

mkvirtualenv jupyter2
workon jupyter2
(jupyter2) pip install jupyter
(jupyter2) ipython kernel install --name "jupyter2_Python_2" --user

last command creates ~/.local/share/jupyter/kernels/jupyter2\ python\ 2/directory

最后一个命令创建~/.local/share/jupyter/kernels/jupyter2\ python\ 2/目录

same stuff for 3

同样的东西 3

mkvirtualenv -p /usr/bin/python3 jupyter3
// this uses python3 as default python in virtualenv
workon jupyter3
(jupyter3) pip install jupyter
(jupyter3) ipython kernel install --name "jupyter3_Python_3" --user

When done you should see both kernels, no matter what env are you using to start jupyter. You can delete links to kernels directly in ~/.local/share/jupyter/kernels/. To specify location provide options to ipython kernel install (--help)or just copy directories from ~/.local/share/jupyter/kernels/to ~/envs/jupyter3/share/jupyterif you want to run multiple kerenels from one notebook only.

完成后,您应该看到两个内核,无论您使用什么 env 来启动 jupyter。您可以直接在~/.local/share/jupyter/kernels/. 要指定位置,如果您只想从一个笔记本运行多个内核,请提供选项ipython kernel install (--help)或仅复制目录。~/.local/share/jupyter/kernels/~/envs/jupyter3/share/jupyter

回答by A T

@singer's solution didn't work for me. Here's what worked:

@singer 的解决方案对我不起作用。这是有效的:

. /path/to/virtualenv/.venv/bin/activate
python -m ipykernel install --user --name .venv --display-name .venv

Reference: Kernels for different environments(official docs)

参考:不同环境的内核(官方文档)

回答by Hamed MP

It is really simple, based on the documentation

这真的很简单,基于文档

You can use a virtualenv for your IPython notebook. Follow the following steps, actually no need for step one, just make sure you activated your virtualenv via source ~/path-to-your-virtualenv/

您可以为 IPython 笔记本使用 virtualenv。按照以下步骤操作,实际上不需要第一步,只需确保您通过以下方式激活了 virtualenvsource ~/path-to-your-virtualenv/

  1. Install the ipython kernel module into your virtualenv

    workon my-virtualenv-name # activate your virtualenv, if you haven't already pip install ipykernel

  2. (The most important step) Now run the kernel "self-install" script:

    python -m ipykernel install --user --name=my-virtualenv-name Replacing the --name parameter as appropriate.

  3. You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kerneland be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

  1. 将 ipython 内核模块安装到您的 virtualenv 中

    workon my-virtualenv-name # 激活你的 virtualenv,如果你还没有 pip install ipykernel

  2. 最重要的一步)现在运行内核“自安装”脚本:

    python -m ipykernel install --user --name=my-virtualenv-name 根据需要替换 --name 参数。

  3. 您现在应该能够在 IPython 笔记本菜单中看到您的内核:Kernel -> Change kernel并能够切换到它(您可能需要在它出现在列表中之前刷新页面)。从那时起,IPython 将记住该笔记本使用哪个内核。

回答by Jigang Hua

the nb_canda is useful:

nb_canda 很有用:

conda install nb_conda

so,you can create and select your own python kernel with conda virtual environment,and manage the packages in venv

因此,您可以使用 conda 虚拟环境创建和选择自己的 python 内核,并在 venv 中管理包

Screenshots

截图

List item

项目清单

Screenshot

截屏

conda environment manager Conda tab in jupyter notebook allows you to manage your environments right from within your notebook.

conda 环境管理器 jupyter notebook 中的 conda 选项卡允许您直接从 notebook 中管理环境。

Change Kernel You can also select which kernel to run a notebook in by using the Change kernel option in Kernel menu

更改内核您还可以使用内核菜单中的更改内核选项来选择在哪个内核中运行笔记本