Python 如何在 Windows 命令行中运行 .py 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19779986/
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 14:36:01  来源:igfitidea点击:

How to run a .py file in windows command line?

pythoncommand-line

提问by MACEE

I have written a simple python program using IDLE to run it from command line. I don't have permission to save .py file in python directory (C:\program files\python33) so I saved it to C:\Pyscripts.

我已经使用 IDLE 编写了一个简单的 python 程序来从命令行运行它。我无权将 .py 文件保存在 python 目录 (C:\program files\python33) 中,所以我将它保存到 C:\Pyscripts。

Also python was already been added to the PATH and I can run a simple print ("Hello")in command line. I have saved this line into a py file and Now I want to run it from command prompt but I have faced different errors! I have searched and tested different ways but they didn't work!

python 也已经被添加到 PATH 中,我可以print ("Hello")在命令行中运行一个简单的。我已将此行保存到 py 文件中,现在我想从命令提示符运行它,但我遇到了不同的错误!我已经搜索并测试了不同的方法,但它们没有用!

any suggestion?

有什么建议吗?

Thanks.

谢谢。

采纳答案by lxx

start cmd.exe

启动cmd.exe

cd C:\Pyscripts

cd C:\Pyscripts

python filename.py

蟒蛇文件名.py

for basics like print 'hello' you don't need any library import statements but for slightly more complex things you will.

对于像 print 'hello' 这样的基础知识,您不需要任何库导入语句,但对于稍微复杂的事情,您将需要。

Which python do you have installed ?

你安装了哪个python?

  1. you don't want to save files in c:\program files under windows. That isn't a good practice. Setting up a dev directory like you did or under your user directory is a much better option.

  2. Have you added python to the path setting ?

  1. 您不想将文件保存在 Windows 下的 c:\program files 中。这不是一个好习惯。像您一样或在用户目录下设置 dev 目录是一个更好的选择。

  2. 您是否将 python 添加到路径设置中?

If you start a command prompt (cmd.exe) not idle and type python what do you get ?

如果您启动一个非空闲的命令提示符 (cmd.exe) 并输入 python,您会得到什么?

If you haven't set the python path http://docs.python.org/2/using/windows.htmlor http://www.katsbits.com/tutorials/blender/setting-up-windows-python-path-system-variable.php

如果您尚未设置 python 路径 http://docs.python.org/2/using/windows.htmlhttp://www.katsbits.com/tutorials/blender/setting-up-windows-python-path -system-variable.php

Just update the settings to the version of python you are using e.g c:\python27

只需将设置更新为您正在使用的 python 版本,例如 c:\python27

  1. Get a text editor instead of using idle. textpad, textedit, vim,emacs ,context, geanie , sublime and many many more.
  1. 获取文本编辑器而不是使用空闲。textpad、textedit、vim、emacs、context、geanie、sublime 等等。

Or get an ide. Aptana's studio 3 is easy to use and free (eclipse with plugins already installed) or the free community version of pycharm

或者得到一个想法。Aptana 的 studio 3 易于使用且免费(已安装插件的 eclipse)或 pycharm 的免费社区版

回答by desired login

The answer to this question is that you should take a basic tutorial on using the interpreter and/or using python modules. For example: http://docs.python.org/2/tutorial/modules.html

这个问题的答案是你应该学习使用解释器和/或使用 python 模块的基本教程。例如:http: //docs.python.org/2/tutorial/modules.html

回答by tdelaney

Here's a simple way to create and run a one-line test script from the command prompt:

这是从命令提示符创建和运行单行测试脚本的一种简单方法:

C:\>cd \Pyscripts
C:\Pyscripts>echo print("hello") > mytest.py
C:\Pyscripts>mytest
hello

Try it and if it fails on your system, post the result. This is a good sanity check for your system configuration.

尝试一下,如果它在您的系统上失败,请发布结果。这是对您的系统配置的一个很好的健全性检查。

(EDIT) Why your original file doesn't work: When you run idle, it starts with an interactive session, something like:

(编辑)为什么您的原始文件不起作用:当您空闲时,它以交互式会话开始,例如:

ActivePython 3.2.2.3 (ActiveState Software Inc.) based on
Python 3.2.2 (default, Sep  8 2011, 10:55:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print("hello")
hello
>>> 

When you save this (File >> Save), you've saved the full shell session output, not just the script you wrote. Instead, you should select (File >> New Window) which will bring up a text editor, add your code and save that. You can use any text editor, not just idle, for this step.

当您保存此文件(文件>>保存)时,您保存了完整的 shell 会话输出,而不仅仅是您编写的脚本。相反,您应该选择 (File >> New Window) 这将打开一个文本编辑器,添加您的代码并保存它。在此步骤中,您可以使用任何文本编辑器,而不仅仅是空闲编辑器。