Reputation: 10476
Say I have the following file t.py
:
from IPython import get_ipython
if __name__ == '__main__':
ip = get_ipython()
if ip is not None:
print('Found IPython')
Here's what happens if I run it, with both Python
and IPython
:
% python t.py
% ipython t.py
Found IPython
Note that, only when running it with ipython
, is get_ipython()
not None.
Is there a way to start an IPython kernel from within the script, so that even if I run it as python t.py
, then get_ipython()
will not be None?
Upvotes: 5
Views: 1608
Reputation: 5601
IPython starts new interactive shell, i.e. your code will be suspended until IPython shell is terminated.
You can have a launcher in a separate file, launcher.py
:
import IPython
if __name__ == '__main__':
IPython.start_ipython(['t.py'])
% python launcher.py
Found IPython
For other options of embedding IPython see the docs https://ipython.readthedocs.io/en/stable/interactive/reference.html#embedding-ipython
Upvotes: 4