【问题标题】:run python command line interpreter with imports loaded automatically运行 python 命令行解释器并自动加载导入
【发布时间】:2012-03-13 15:56:34
【问题描述】:

我想在 python 解释器中玩耍,但完成了一堆导入和对象设置。现在我正在命令行上启动解释器并每次都进行设置工作。有什么方法可以在完成所有初始化工作后启动命令行解释器?

例如:

# Done automatically.
import foo
import baz
l = [1,2,3,4]
# Launch the interpreter.
launch_interpreter()
>> print l
>> [1,2,3,4]

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以使用您希望自动运行的代码创建一个脚本,然后使用python -i 运行它。例如,使用以下代码创建一个脚本(我们称之为 script.py):

    import foo
    import baz
    l = [1,2,3,4]
    

    然后运行脚本

    $ python -i script.py
    >>> print l
    [1, 2, 3, 4]
    

    脚本完成运行后,python 会让您进入一个交互式会话,脚本的结果仍然存在。

    如果您真的希望在运行 python 时每次完成一些事情,您可以将环境变量 PYTHONSTARTUP 设置为每次启动 python 时都会运行的脚本。请参阅interactive startup file 上的文档。

    【讨论】:

    • 交互式启动文件的文档现在在这里:docs.python.org/3/tutorial/…
    • 不需要特殊文件。 python -i -c "import sys; import mymodule; a=5" 选项-c 允许您指定内联代码。选项-i 保持交互模式。
    【解决方案2】:

    我使用PYTHONSTARTUP

    我的.bash_profile 有一个指向我的主文件夹.pyrc 的路径,作为其中的导入语句。

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP

    【讨论】:

      【解决方案3】:

      我在尝试为我的研究配置新的desk 时遇到了这个问题,发现上面的答案不太符合我的愿望:将整个桌面配置包含在一个文件中(这意味着我不会创建@srgerg 建议的单独的script.py)。

      这就是我最终实现目标的方式:

      export PYTHONPATH=$READ_GEN_PATH:$PYTHONPATH
      
      alias prepy="python3 -i -c \"
      from naive_short_read_gen import ReadGen
      from neblue import neblue\""
      

      在这种情况下,neblue 位于 CWD 中(因此不需要路径扩展名),而 naive_short_read_gen 位于我系统上的任意目录中,该目录通过 $READ_GEN_PATH 指定。

      如有必要,您可以在一行中执行此操作:alias prepy=PYTHONPATH=$EXTRA_PATH:$PYTHONPATH python3 -i -c ...

      【讨论】:

        【解决方案4】:

        我想我知道你想做什么。您可能想要检查 IPython,因为如果不提供 -i 选项(至少不直接),您将无法启动 python 解释器。 这就是我在项目中所做的:

        def ipShell():
            '''Starts the interactive IPython shell'''
            import IPython
            from IPython.config.loader import Config
            cfg = Config()
            cfg.TerminalInteractiveShell.confirm_exit = False
            IPython.embed(config=cfg, display_banner=False)
        # Then add the following line to start the shell
        ipShell()
        

        不过,您需要小心,因为 shell 将具有定义函数 ipShell() 的模块的命名空间。如果您将定义放在您运行的文件中,那么您将能够访问您想要的globals()。可能有其他解决方法来注入您想要的命名空间,b̶u̶t̶̶y̶o̶u̶̶w̶o̶u̶l̶d̶̶h̶a̶v̶e̶̶t̶o̶̶g̶i̶v̶e̶̶a̶r̶g̶u̶m̶e̶n̶t̶s̶̶t̶o̶̶t̶h̶t̶i̶o̶n̶̶t̶h̶t̶i̶o̶n̶̶t̶h̶n̶t

        编辑

        以下函数默认调用者的命名空间(__main__.__dict__)。

        def ipShell():
            '''Starts the interactive IPython shell
            with the namespace __main__.__dict__'''
            import IPython
            from __main__ import __dict__ as ns
            from IPython.config.loader import Config
            cfg = Config()
            cfg.TerminalInteractiveShell.confirm_exit = False
            IPython.embed(config=cfg, user_ns=ns, display_banner=False)
        

        没有任何额外的参数。

        【讨论】:

          【解决方案5】:

          您可以在启动命令行时使用 -s 选项。详细信息在文档here

          中给出

          【讨论】:

          • 这是用于 IDLE,而不是用于普通控制台会话,不是吗?
          猜你喜欢
          • 2011-10-27
          • 2016-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多