如何通过运行'python ...'从shell启动ipython?

Shw*_*chk 16 python shell command-line ipython python-3.x

我想在python启动代码中添加一些命令行选项,以便实际调用ipython shell.我怎么做?

rgt*_*gtk 37

要在Python中直接启动IPython shell:

from IPython import embed

a = "I will be accessible in IPython shell!"

embed()
Run Code Online (Sandbox Code Playgroud)

或者,只需从命令行运行它:

$ python -c "from IPython import embed; embed()"
Run Code Online (Sandbox Code Playgroud)

embed 将使用shell中的所有局部变量.

如果要提供自定义本地(shell中可访问的变量),请查看 IPython.terminal.embed.InteractiveShellEmbed


col*_*yre 13

您可以首先安装适合您的特定版本的 IPython,然后使用模块名称启动 Python,例如:

$ 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]:
Run Code Online (Sandbox Code Playgroud)

因此,您甚至可以安装多个 Python 版本,并分别为每个版本启动 IPython 解释器。为了方便起见,下一步是给命令起别名,例如.bashrc

alias ipython3.7='python3.7 -m IPython'
Run Code Online (Sandbox Code Playgroud)

然后您可以轻松启动特定版本的 IPython:

$ 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]:
Run Code Online (Sandbox Code Playgroud)

另请参阅https://github.com/ipython/ipython#development-and-instant-running

Python3.12 的编辑:我遇到了一些问题,以这种方式在 Python3.12(来自 deadsnakes)下安装 IPython。根本问题是pkgutil不知道ImpImporter,它已被弃用,这意味着我的 Python3.12 pip 安装已损坏。感谢ensurepip,解决方案很简单:python3.12 -m ensurepip --upgrade。然后python3.12 -m pip install IPython应该可以工作。


Sve*_*ach 6

要完全按照您的要求执行操作,即将命令行选项添加python到实际调用IPython的调用中,您可以执行以下操作:

python -c 'import subprocess; subprocess.call("ipython")'
Run Code Online (Sandbox Code Playgroud)

但是,我无法想象任何有用的情况.


Ram*_*nez 5

ipython也许一个选项只是像这样嵌入到您的代码中

def some_function():
    some code

    import IPython
    IPython.embed()
Run Code Online (Sandbox Code Playgroud)

当您在某些代码中运行该函数时,它将启动并ipython终止,其范围是调用该函数的函数之一。