How to run a Python script from IDLE command line?

风流意气都作罢 提交于 2019-12-06 05:12:21

问题


In a bash shell, I can use 'bash ' or 'source ' to invoke a script by hand. Can I do the similar thing in the Python IDLE's interactive shell? I know I can go to File >> Open Module, then run it in a separate window, but that's troublesome.


回答1:


One method I have found is execfile. It has the signature execfile(<filename>,<globals>,<locals>) and runs the file in the same thread as the current IDLE Session. This method is Python 2 specific

The method the OP is asking for (in a different thread/window) would be to use subprocess to run in a different thread/window.

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'])

These essentially do what nneonneo's answer does, but using subprocess to execute it in a different thread.




回答2:


You can just run a Python script from the commandline:

python <script.py>


来源:https://stackoverflow.com/questions/15399444/how-to-run-a-python-script-from-idle-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!