Execute Python script within Jupyter notebook using a specific virtualenv
Question: 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!


Answer: I found this link to be very useful:

<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:

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`

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

    
    
    {
      "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.

