【问题标题】:Python os.system('python') only works in the shellPython os.system('python') 仅适用于 shell
【发布时间】:2017-11-29 13:50:29
【问题描述】:

我想通过os.system('python')来访问python shell的cmd版本 但是,这仅适用于 python shell,而不适用于程序。考虑到如果我想使用os.system('cmd')它将从程序中运行,这尤其令人困惑。我在做什么,无法通过程序中的命令打开 python shell?为什么它对 cmd 有效?

【问题讨论】:

  • 澄清一下,您是否尝试从 python 脚本中打开交互式 python shell?
  • 我不知道; os.system("python") 在交互式和脚本(程序)模式下对我来说都很好。你能发布你的代码和你得到的实际输出吗?我跟着我的输入命令,以确保在我寻找解决方案时不会关闭任何东西。
  • 为我工作。但是,我使用的是 Linux。也许 Windows 在将终端窗口与 Python 关联时遇到问题?
  • 大概你在 Windows 上?我刚刚在 Fedora 24 上试了一下,没有问题。
  • 是的,我在 Windows 上。

标签: python shell cmd


【解决方案1】:

我想你有一个 python 脚本,并且你希望能够在脚本执行期间的某个时间点打开一个交互式 python shell。如果这是您想要的,那么这应该可以:

import code

# do some stuff
# open interactive python shell using the code library
code.interact(local=locals())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2018-07-06
    • 1970-01-01
    • 2019-08-09
    • 2019-03-15
    • 1970-01-01
    • 2016-09-14
    • 2016-10-20
    相关资源
    最近更新 更多