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?
我想在python启动代码中添加一些命令行选项,以便实际调用ipython shell。我怎么做?
5 个解决方案
#1
27
To start IPython shell directly in Python:
要在Python中直接启动IPython shell:
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.
embed将使用shell中的所有局部变量。
If you want to provide custom locals (variables accessible in shell) take a look at IPython.terminal.embed.InteractiveShellEmbed
如果要提供自定义本地(shell中可访问的变量),请查看IPython.terminal.embed.InteractiveShellEmbed
#2
6
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调用以实际调用IPython,您可以执行以下操作:
python -c 'import subprocess; subprocess.call("ipython")'
I can't imagine, though, any circumstances where this would be useful.
但是,我无法想象任何有用的情况。
#3
3
Maybe an option is just to embed ipython
in your code like this
也许一个选项只是在你的代码中嵌入ipython就像这样
def some_function():
some code
import IPython
IPython.embed()
When you run the function in some code it will launch and ipython
terminal whose scope is the one of the function from where it was called.
当您在某些代码中运行该函数时,它将启动并且ipython终端的范围是调用它的函数之一。
#4
2
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.
“python启动代码”的含义并不完全清楚;我假设这是指用于启动Python的shell代码。
On Unix, you could use alias
to substitute one command for another:
在Unix上,您可以使用别名将一个命令替换为另一个命令:
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.
如果这不是您想要的,请澄清您的问题。
#5
1
I think you mean something like python C:\Python27\Scripts\ipython-script.py
我认为你的意思是像python C:\ Python27 \ Scripts \ ipython-script.py
#1
27
To start IPython shell directly in Python:
要在Python中直接启动IPython shell:
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.
embed将使用shell中的所有局部变量。
If you want to provide custom locals (variables accessible in shell) take a look at IPython.terminal.embed.InteractiveShellEmbed
如果要提供自定义本地(shell中可访问的变量),请查看IPython.terminal.embed.InteractiveShellEmbed
#2
6
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调用以实际调用IPython,您可以执行以下操作:
python -c 'import subprocess; subprocess.call("ipython")'
I can't imagine, though, any circumstances where this would be useful.
但是,我无法想象任何有用的情况。
#3
3
Maybe an option is just to embed ipython
in your code like this
也许一个选项只是在你的代码中嵌入ipython就像这样
def some_function():
some code
import IPython
IPython.embed()
When you run the function in some code it will launch and ipython
terminal whose scope is the one of the function from where it was called.
当您在某些代码中运行该函数时,它将启动并且ipython终端的范围是调用它的函数之一。
#4
2
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.
“python启动代码”的含义并不完全清楚;我假设这是指用于启动Python的shell代码。
On Unix, you could use alias
to substitute one command for another:
在Unix上,您可以使用别名将一个命令替换为另一个命令:
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.
如果这不是您想要的,请澄清您的问题。
#5
1
I think you mean something like python C:\Python27\Scripts\ipython-script.py
我认为你的意思是像python C:\ Python27 \ Scripts \ ipython-script.py