使用特定的virtualenv在Jupyter Notebook中执行Python脚本 [英] Execute Python script within Jupyter notebook using a specific virtualenv
问题描述
我想从Jupyter笔记本中执行长时间运行的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,brew版本的python.如果您使用的是系统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笔记本(此步骤无需激活venv)
then start jupyter notebook (the venv need not be activated for this step)
(py2env)$ jupyter notebook
# or
#$ jupyter notebook
在jupyter笔记本下拉菜单中:Kernel >> Change Kernel >> <list of kernels>
,您应该看到Python2 (py2env)
内核
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
here is the link to detail docs http://ipython.readthedocs.io/en/stable/install/kernel_install.html
这篇关于使用特定的virtualenv在Jupyter Notebook中执行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!