[英]How to execute a Python program from the Python shell?
I am very very new in Python and I have a doubt. 我对Python非常陌生,我对此表示怀疑。
If I write a program in a text editor (such as Nodepad++), then can I execute it from the Python shell (the one that begin with >>)? 如果我在文本编辑器(例如Nodepad ++)中编写程序,那么可以从Python shell(以>>开头的程序)执行该程序吗? What command have I to launch to execute my Python program?
我要启动什么命令来执行我的Python程序?
Tnx 特纳克斯
Andrea 安德里亚
From the Python console, you can run 在Python控制台中,您可以运行
execfile('program.py')
where program.py
is the path to your file. 其中
program.py
是文件的路径。
EDIT: 编辑:
In Python 3, you'd have to define execfile
yourself before you could use it. 在Python 3中,您必须先定义
execfile
然后才能使用它。 Copy and paste the following. 复制并粘贴以下内容。
def execfile(path, globals=None, locals=None):
with open(path, "r") as file:
exec(file.read(), globals, locals)
You specifically asked for running it from the Python prompt, but if possible, consider running it from the normal command prompt (DOS, bash, etc.) It's a little easier, and more normal. 您专门要求从Python提示符下运行它,但如果可能的话,请考虑从普通的命令提示符下(DOS,bash等)运行它。这更容易一些,并且更加正常。
you are done 你完成了
from within the Python IDLE shell: 从Python IDLE shell中:
File -> Open... -> Select your Python program 文件->打开...->选择您的Python程序
When your program has openend select Run -> Run Module or press F5 当程序有openend时,选择Run-> Run Module或按F5
In the view of mine: you wrote a program: test.py 在我看来:您编写了一个程序:test.py
print 'test file' 打印“测试文件”
and you turn to the windows cmd: you excuted python,and you got this 然后转到Windows cmd:您检出了python,然后您得到了
>
>
then you can just simply: 那么您可以简单地:
os.system('python test.py')
os.system('python test.py')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.