I would like to add some commandline options to a python launch code in order to actually invoke an ipython shell. How do I do that?
-
11Could you explain your real problem? I mean, which actual problem is this hack supposed to solve?Sven Marnach– Sven Marnach2011-12-13 15:03:24 +00:00Commented Dec 13, 2011 at 15:03
-
I use an IDE that has a limited support for an interactive shell to run your scripts in (pycharm), and I would like to be able to run the scripts in a more interactive way, such as in IEP.Shwouchk– Shwouchk2011-12-13 21:39:00 +00:00Commented Dec 13, 2011 at 21:39
-
The fix should work in windows environments as wellShwouchk– Shwouchk2011-12-13 21:41:47 +00:00Commented Dec 13, 2011 at 21:41
-
Does your license cover upgrading to the newly released PyCharm 2? They've added IPython support: jetbrains.com/pycharm/whatsnew/index.htmlThomas K– Thomas K2011-12-13 22:38:24 +00:00Commented Dec 13, 2011 at 22:38
-
It does, the support is only in the python 'console' and not when running scripts. Also, I was unable to find how to enable ipython instead of python...Shwouchk– Shwouchk2011-12-14 07:32:37 +00:00Commented Dec 14, 2011 at 7:32
7 Answers
To start IPython shell directly in Python:
from IPython import embed
a = "I will be accessible in IPython shell!"
embed()
Or, to simply run it from command line:
$ python -c "from IPython import embed; embed()"
embed will use all local variables inside shell.
If you want to provide custom locals (variables accessible in shell) take a look at IPython.terminal.embed.InteractiveShellEmbed
1 Comment
You can first install IPython for your specific version and then start Python with module name, e.g.:
$ python3.7 -m pip install IPython
$ python3.7 -m IPython
Python 3.7.7 (default, Mar 10 2020, 17:25:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
So you can even have multiple Python versions installed and start an IPython interpreter for each version separately. A next step for convenience would be to alias the command, e.g. in .bashrc:
alias ipython3.7='python3.7 -m IPython'
Then you can easily start IPython for the specific version(s):
$ ipython3.7
Python 3.7.7 (default, Mar 10 2020, 17:25:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
See also https://github.com/ipython/ipython#development-and-instant-running.
EDIT for Python3.12: I had some issues, getting IPython installed under Python3.12 (from deadsnakes) this way. The underlying problem was that pkgutil does not know ImpImporter, which is deprecated, meaning that I had a broken pip installation for Python3.12. Thanks to ensurepip, the solution is simple: python3.12 -m ensurepip --upgrade. Then python3.12 -m pip install IPython should work.
Comments
To do exactly what you asked for, i.e. add command line options to a python invocation to actually invoke IPython, you can do this:
python -c 'import subprocess; subprocess.call("ipython")'
I can't imagine, though, any circumstances where this would be useful.
3 Comments
It is not entirely clear what you mean by "a python launch code"; I assume this refers to the shell code you use to launch Python.
On Unix, you could use alias to substitute one command for another:
aix@aix:~$ alias python=ipython
aix@aix:~$ python
Enthought Python Distribution -- http://www.enthought.com
Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Nov 29 2010, 13:51:37)
Type "copyright", "credits" or "license" for more information.
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
If this is not what you want, please clarify your question.
1 Comment
alias ipython="python -c 'from IPython import embed; embed()'"no ... we need to be able to run a cell from a python script on the ipython kernal and return the output from the function sent < hence the function would be sent as a string to the ipython(ikernal) or jupyter_client and return the value , pass/ fail / output ... verbose on/off....
so to use the jupyter_client to even execute a script on the operating system using a magic command sending it to the jupyter client to return the result could be the output from an installation via pip or even a project directory listing....but also it can be a code output , hence also it could be a custom environmnet ( loaed in a virtual jupiter notebook, items imported , code executed , result returned) ...in a clean environment (could be volitile and destructable hence not held or kepy , as well as could be a prewritten jupyter notebook file , or simply a python script?).. today we recive a lot of mixed scripts containing markdown and code and often we do not have time to sift through documents or keep writing mad funcitons to extract specific code fragments from code .. but ipython can handle these mised code snipptes of markdown and pycode or other lisp/rust/r/ etc which can all be ru on a note book ... hence from a python app r code can be executed in a cell and its output returned to the original python app ! hence the requirement for this funcitonality as it enables mulit code develoopment strategeies :
Today this is also used in fucntion calling for llm large lang models etc !