On 30 October 2017 at 20:35, Erik Bray <erik.m.b...@gmail.com> wrote:

> I should add--another case that is becoming extremely common is
> beginners learning Python for the first time inside the
> Jupyter/IPython Notebook.  And in my experience it can be very
> difficult for beginners to understand the connection between what's
> happening in the notebook ("it's in the web-browser--what does that
> have to do with anything on my computer??") and the underlying Python
> interpreter, file system, etc.  Being able to pip install from within
> the Notebook would be a big win.  This is already possible since
> IPython allows running system commands and it is possible to run the
> pip executable from the notebook, then manually restart the Jupyter
> kernel.
>
> It's not 100% clear to me how my proposal below would work within a
> Jupyter Notebook, so that would also be an angle worth looking into.
>

A few specific notes here:

1. As you say, this sort of already works in notebooks, since instructors
can say to run "!pip install requests" and then restart the language kernel.
2. We could probably replicate that style in IDLE, since that runs user
code in a subprocess, similar to the way Jupyter language kernels are
separate from the frontend client
3. We can't replicate it as readily in the regular REPL, since that runs
Python code directly in the current process, but even there I believe we
could potentially trigger a full process restart via execve (or the C++
style _execve on Windows)

(We'd want a real process restart, rather than emulating it by calling
Py_Initialize & Py_Finalize multiple times, as not every module properly
supports multiple initialise/finalise cycles within a single process, and
module-specific quirks are exactly what we'd be trying to avoid by forcing
an interpreter restart)

So the main missing piece if we went down that path would be to offer a way
to say from within the interpreter itself "Restart the current interactive
session". One possible approach to that would be to define a
RestartInterpreter subclass of SystemExit, which the interpreter would
intercept at around the same point where it checks for the PYTHONINSPECT
flag, and then initiate a graceful process shutdown and restart, rather
than a normal exit. We'd probably want that capability to be off by default
and enable it explicitly from the CPython CLI though, as otherwise it could
have some really annoying side effects in runtime embedding use cases.

I'm sure there'd be some thorny edge cases that would arise in trying to
make this work in practice, but at first glance, the general idea sounds
potentially feasible to me.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to