【问题标题】:How to start a Python ipython shell, automatically run inside it a few commands, and leave it open?如何启动 Python ipython shell,在其中自动运行一些命令,并保持打开状态?
【发布时间】:2010-06-14 04:51:26
【问题描述】:

我试过了

echo "print 'hello'" | ipython

运行命令但 ipython 立即退出。

有什么想法吗?谢谢!

编辑: 我实际上需要将命令传递到交互式 Django shell,例如:

echo "print 'hello'" | python manage.py shell

所以建议的 -i 开关 gimel 似乎不起作用(shell 在执行后仍然退出)

【问题讨论】:

    标签: python bash shell ipython


    【解决方案1】:

    使用与标准解释器相同的标志,-i

    -i

    当脚本作为第一个参数传递或使用 -c 选项时,在执行脚本或命令后进入交互模式,即使 sys.stdin 看起来不是终端。未读取 PYTHONSTARTUP 文件。

    一个 Linux 示例,使用 -c command line flag:

    $ ipython -i -c 'print "hello, ipython!"'
    hello, ipython!
    
    In [2]: print "right here"
    right here
    
    In [3]:
    

    【讨论】:

    • 好吧,我骗了一点,我的实际 python 命令已经是“python manage.py shell”,用于启动交互式 Django shell。所以我看不到 -i 解决方案如何为我工作。
    【解决方案2】:

    尝试在 ~/.ipython 中使用 ipy_user_conf.py

    【讨论】:

      【解决方案3】:

      我不确定 ipython,但基本的 python 解释器有一个命令行参数,可以在它执行你给它的文件后给你提示。我没有方便的解释器来告诉你它是什么,但你可以使用python --help 得到它。它应该完全符合您的要求。

      【讨论】:

        【解决方案4】:

        运行custom startup script/profile script with the Django shell was marked as closed: wontfix

        但是,该票中讨论了一个 shell_plus Django 扩展,它似乎可以满足您的需求。我还没有机会检查它,但它看起来至少可以运行负载以自动导入所有已安装应用程序的所有模型(我经常这样做。我发现自己正在这样做)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-03
          相关资源
          最近更新 更多