Pergunta

How can I do the equivalent of ipython notebook and ipython profile from inside a python script? It should be straightforward but I can't track down the right invocation. (Inter alia I blindly tried IPython.start_kernel() and from IPython.extensions import notebook and variants, but had no luck so far.)

In my case, I can't just launch a subprocess and execute ipython notebook: I'm on a weird configuration where I can run python from the Start menu (Windows 7), but not from the command line or from the script. (To be completely clear: I do know the location of the python executable, but am restricted from executing it directly).

Foi útil?

Solução

After some sleuthing and reflection, I decided the best solution is to let IPython start notebook itself. So the problem is to simulate what happens when calling ipython from the command line:

% ipython notebook <directory>

IPython is started from the command line via a short python script. I import it but make it think it's run as the __main__ module:

import sys, imp
ipython_path = r"/path/to/ipython-script"

sys.argv = [ ipython_path, "notebook" ]
_ipython = imp.load_source('__main__', ipython_path)

To serve a directory different from the current directory, just add it to sys.argv as an additional argument:

sys.argv = [ ipython_path, "notebook", "path/to/notebooks" ]

Where is the commandline ipython script? On Windows, IPython is launched with the help of ipython_script.py in the Scripts directory (e.g., C:\Python27\Scripts\ipython_script.py). On OS X, it can be launched by the python script Library/Python/2.7/bin/ipython. (I installed IPython via easy_install; I suppose there might be other configurations.) You can track down your python installation like this:

import IPython
import inspect
inspect.getsourcefile(IPython)

This is the path to the module, not the launcher script. The script will be in a nearby directory.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top