在bash shell中,我可以使用'bash'或'source'来手动调用脚本。我可以在Python IDLE的交互式shell中做类似的事情吗?我知道我可以去File >> Open Module,然后在另一个窗口中运行它,但这很麻烦。如何从IDLE命令行运行Python脚本?
回答
您只需从命令行运行Python脚本:
python <script.py>
一种方法,我发现是execfile
。它具有签名execfile(<filename>,<globals>,<locals>)
并在与当前IDLE会话相同的线程中运行该文件。 此方法是Python 2特定的
OP所要求的方法(在不同的线程/窗口中)将使用subprocess
在不同的线程/窗口中运行。
import subprocess #any of these will run the file. Pick the one that best suits you. subprocess.call(['python','filename.py']) subprocess.check_call(['python','filename.py']) subprocess.Popen(['python','filename.py'])
这些基本上是做nneonneo的回答做了什么,但使用subprocess
在不同的线程来执行它。
execfile()仅适用于Python 2.x.它已在Python 3中被删除:http://python3porting.com/differences.html,其中可以使用'exec(open(thefile).read())',它将输出返回到IDLE交互式shell,但是我还没有想出一种方法来传递命令行参数。子进程将工作,但它不会返回输出,并且需要更多的麻烦来获得它(尽管可能是可行的)。这与“。”相比毫无意义。运算符在bash中执行脚本。太糟糕了。 – 2013-03-14 01:35:43
@HaiXinTie谢谢你的注意。我已经更新了答案以反映这一事实。 – 2013-03-14 01:37:32
- 1. 如何从命令行运行脚本?
- 2. 如何在IDLE在Windows环境下运行命令行python脚本
- 3. 从MEL命令运行的Python脚本
- 4. 从python脚本运行vim命令
- 5. 从命令行启动Python IDLE编辑脚本
- 6. 如何从脚本运行raco命令?
- 7. 如何从命令行运行多个Python脚本?
- 8. Python的 - 如何从IDLE运行模块与命令提示符
- 9. 如何从命令行将Java脚本运行Groovy脚本?
- 10. Python脚本可以在从IDLE运行但不从命令行运行时看到Y:/ Drive
- 11. 从命令行运行Perl脚本?
- 12. 从命令行运行Groovy脚本
- 13. 从命令行运行Sqlite3脚本
- 14. 从vim命令行运行vim脚本
- 15. 从命令行执行python脚本,Linux
- 16. 如何从python脚本运行SVN命令?
- 17. 运行bash命令到python脚本
- 18. 通过python脚本运行ffmpeg命令
- 19. Python脚本来运行Django的命令
- 20. 在python脚本中运行cmake命令
- 21. 从命令行运行Python
- 22. 如何在django视图中运行命令行python脚本?
- 23. 命令从命令行运行,但不能从shell脚本运行
- 24. 从python(Django)shell中的python脚本运行python命令。 Django
- 25. gdb从命令行运行,但不是从脚本运行
- 26. Powershell脚本从命令行运行,但不是从TFS运行
- 27. bash脚本从命令行运行但不是从cron运行
- 28. 脚本在命令运行时运行
- 29. 使用Enthought Canopy从Windows命令行运行python脚本
- 30. 使用命令行参数从C#运行Python脚本
我认为提交者正在寻找一种方法在交互式shell内部执行,而不仅仅是命令行 – 2013-03-14 01:13:59