Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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)?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

first activate virtualenv

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

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)

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...