【问题标题】:How to execute a python file using cmd in Spyder?如何在 Spyder 中使用 cmd 执行 python 文件?
【发布时间】:2020-07-15 11:16:19
【问题描述】:

有一个文件 a.py 在 Spyder(python) 中运行,我想在 cmd 中运行 b.py,它应该由 a.py 调用,因此 b.py 文件在命令行和 b.py 中运行文件有代码来获取输入。 a = int(input())

如何在命令提示符下从 a.py 文件运行此 b.py 文件?

【问题讨论】:

    标签: python python-3.x shell command


    【解决方案1】:

    看起来你想从 a.py 运行一个文件 b.py 所以, 您可以简单地在 a.py 中导入 b.py 并使用 b.py 中的函数

    例如, 假设b.py中有一个函数show()

    def show():
        print("This is from file B")
    

    现在,将该文件导入到 a.py 中

    import b
    
    print("This is from file A")
    b.show()
    

    否则, 您可以使用另一种使用 os 模块的方法

    import os 
    os.system('python b.py')
    

    通过添加这两行,您可以从 a.py 运行 b.py

    【讨论】:

    • 但是上面的程序不能得到输入它只是打印的东西..有什么办法也可以得到输入
    • 您可以将 a = int(input()) (在 b.py 中)包装在一个函数中,然后在 a.py 中调用该函数,现在您可以从 cmd 获取输入
    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2022-08-10
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    相关资源
    最近更新 更多