Python 如何在命令提示符下执行/调用 .py 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18579650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:08:41  来源:igfitidea点击:

How to execute/call on a .py file in command prompt?

pythoneclipsepython-3.xpydev

提问by Asher

I have written a .py file using Eclipse. I want to run that file in command prompt. How do I go about that? I'm running it on Windows.

我已经使用 Eclipse 编写了一个 .py 文件。我想在命令提示符下运行该文件。我该怎么做?我在 Windows 上运行它。

I tried python D:\Python Work/class/src/hello.py The error I received was python: can't open file 'D:\Python':[Errno 2] No such file or directory. Also 'D:\Python' is not the entire address that I had specified.

我试过 python D:\Python Work/class/src/hello.py 我收到的错误是 python: can't open file 'D:\Python':[Errno 2] No such file or directory。另外 'D:\Python' 不是我指定的整个地址。

Python was already been added to the PATH. I tried doing cd[file directory] and then python file.py but that gave me the same error

Python 已经被添加到 PATH 中。我试着做 cd[file directory] ​​然后 python file.py 但这给了我同样的错误

Thanks

谢谢

采纳答案by Steve Barnes

Assuming you are running on windows - either:

假设您在 Windows 上运行 - 要么:

python "D:\Folder name\Project name\src\module name.py"

Or:

或者:

python "D:/Folder name/Project name/src/module name.py"

Your choice basically.

你的选择基本上。

The rules for windows file names are:

windows文件名的规则是:

Back slashes, \must be escaped by doubling to \\spaces mustbe inside of double quotes "like this" quotes and few others need to be escaped as well and the extension doesmatter so give it.

反斜杠,\必须通过加倍到\\空格来转义必须在双引号“像这样”引号内,并且很少有其他人需要转义,并且扩展名确实很重要,所以给它。