【问题标题】:How can I launch ipython from shell, by running 'python ...'?如何通过运行“python ...”从 shell 启动 ipython?
【发布时间】:2012-01-19 10:49:15
【问题描述】:

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

【问题讨论】:

  • 您能解释一下您的真正问题吗?我的意思是,这个 hack 应该解决哪个实际问题?
  • 我使用的 IDE 对交互式 shell 的支持有限,以便在 (pycharm) 中运行您的脚本,并且我希望能够以更具交互性的方式运行脚本,例如在个人教育计划。
  • 该修复程序也应该适用于 Windows 环境
  • 您的许可证是否涵盖升级到新发布的 PyCharm 2?他们添加了 IPython 支持:jetbrains.com/pycharm/whatsnew/index.html
  • 确实如此,支持仅在 python 'console' 中,而不是在运行脚本时。另外,我找不到如何启用 ipython 而不是 python...

标签: python shell command-line python-3.x ipython


【解决方案1】:

直接在 Python 中启动 IPython shell:

from IPython import embed

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

embed()

或者,简单地从命令行运行它:

$ python -c "from IPython import embed; embed()"

embed 将使用 shell 内的所有局部变量。

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

【讨论】:

  • 我什至不再使用那个环境或操作系统,但我认为这会奏效。谢谢!
【解决方案2】:

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

python -c 'import subprocess; subprocess.call("ipython")'

不过,我无法想象在任何情况下这都会有用。

【讨论】:

  • 如果我错了,请纠正我,但是这样传递的任何命令和脚本文件仍将在 python 而不是 ipython 中运行,对吧?
  • @Shwouchk:您最初的问题提供的信息很少——这个答案是我能给出的最好的猜测。从 cmets 到您最初的问题,我认为这个解决方案对您没有帮助,但是尝试一下怎么样?
  • 这实际上解决了我从 emacs 提示符运行 ipython 的相关问题,否则我无法正确地将输出刷新到 emacs 缓冲区。
【解决方案3】:

您可以先为您的特定版本安装 IPython,然后再安装start Python with module name,例如:

$ 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]:

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

alias ipython3.7='python3.7 -m IPython'

然后您可以轻松启动特定版本的 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]:

另见https://github.com/ipython/ipython#development-and-instant-running

【讨论】:

    【解决方案4】:

    也许一个选项就是像这样在你的代码中嵌入ipython

    def some_function():
        some code
    
        import IPython
        IPython.embed()
    

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

    【讨论】:

      【解决方案5】:

      您所说的“python 启动代码”是什么意思并不完全清楚;我假设这是指您用来启动 Python 的 shell 代码。

      在 Unix 上,您可以使用 alias 将一个命令替换为另一个:

      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]: 
      

      如果这不是您想要的,请澄清您的问题。

      【讨论】:

      • 此外,这并不总是与您的 python 可执行文件匹配。 alias ipython="python -c 'from IPython import embed; embed()'"
      【解决方案6】:

      我想你的意思是python C:\Python27\Scripts\ipython-script.py

      【讨论】:

        最近更新 更多