【问题标题】:How to start a shell from a python script如何从 python 脚本启动 shell
【发布时间】:2020-11-16 13:30:39
【问题描述】:

我想运行一个脚本,它会进行一些设置,然后在该环境中打开一个 shell。所以与其做

$ python
>>> # do some setup
>>> # start doing what I really came here to do

我想做

$ python my_script.py
>>> # start doing what I really came here to do

【问题讨论】:

  • 你可以编写文件,然后在shell中输入from filename import *
  • 这能回答你的问题吗? stackoverflow.com/questions/22163422/…
  • @gmdev 也许,但到目前为止我的答案是简洁和相关的

标签: python


【解决方案1】:

使用-i 参数运行您的脚本:

python -i my_script.py

这将执行my_script.py 并随后进入交互式shell。

【讨论】:

    【解决方案2】:

    你可以这样做

    import code
    
    variables = {"test": True}
    shell = code.InteractiveConsole(variables)
    shell.interact()
    

    现在它将打开python shell,您可以直接访问测试变量

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2019-03-16
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多