使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本 [英] Execute Python script within Jupyter notebook using a specific virtualenv

查看:33
本文介绍了使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

该脚本具有许多依赖项和命令行参数,并使用特定的 virtualenv 执行.是否可以从指定的 virtualenv(与 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)?

谢谢!

推荐答案

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

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

首先激活virtualenv

first activate virtualenv

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

if starting afresh then, e.g., you could use 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

接下来,设置内核

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

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

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

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

在 jupyter notebook 下拉菜单中:Kernel >>更改内核 >><内核列表> 你应该看到 Python2 (py2env) kernel

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

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

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

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

这篇关于使用特定的 virtualenv 在 Jupyter notebook 中执行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆