从 Windows 命令行运行 Python

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

Running Python from the Windows Command Line

pythonwindows

提问by dave.j.lott

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?

如何从 Windows 命令行 ( cmd.exe)运行 Python 文件,以便不必每次都重新输入代码?

回答by quip

Wouldn't you simply save your Python code into a file, and then execute that file using Python?

您不是简单地将 Python 代码保存到文件中,然后使用 Python 执行该文件吗?

Save your code into a file called Test.py.

将您的代码保存到名为Test.py.

And then run it?

然后运行它?

$ C:\Python24\Python.exe C:\Temp\Test.py

回答by RedGlyph

If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.

如果您不想安装 IDE,也可以使用包含 Python 编辑器和控制台的 IDLE 进行测试,这是标准安装的一部分。

If you installed the python.org version, you will see an IDLE (Python GUI)in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.

如果您安装了 python.org 版本,您将IDLE (Python GUI)在开始菜单中看到。我建议将它添加到您的快速启动或桌面 - 无论您最熟悉什么。然后右键单击您创建的快捷方式并将“开始于”目录更改为您的项目目录或您可能会遇到的地方,而不是安装目录,这是默认位置,可能是一个坏主意。

When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.

当您双击快捷方式时,它将启动 IDLE,这是一个控制台,您可以在其中键入 Python 命令并具有历史记录、完成、颜色等。您还可以启动编辑器来创建程序文件(如其他帖子中所述)。甚至还有一个调试器。

If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test(if that is a module), or finally from the debugger.

如果您将应用程序保存在“test.py”中,则可以从编辑器本身启动它。或者从带有execfile("test.py"),的控制台import test(如果这是一个模块),或者最后从调试器。

回答by Thomas Owens

If you put the Python executable (python.exe) on your path, you can invoke your script using python script.pywhere script.py is the Python file that you want to execute.

如果您将 Python 可执行文件 (python.exe) 放在您的路径上,您可以使用python script.py其中的 script.py 是您要执行的 Python 文件来调用您的脚本。

回答by Eric O Lebigot

A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.

一个很好的工具是IPython shell。它不仅可以运行您的程序(%run 命令),而且还提供了许多以高效方式交互使用 Python 的工具(自动完成、语法着色、快速访问文档、与 Matplotlib 的良好交互……)。安装后,您将可以在“开始”菜单中访问其外壳。

回答by BenjaminRH

You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables

您需要创建环境变量。按照此处的说明操作:http: //www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables

回答by Geo

Open a command prompt, by pressing Win+Rand writing cmdin that , navigate to the script directory , and write : python script.py

打开命令提示符,按Win+R并写入该命令cmd,导航到脚本目录,然后写入:python script.py

回答by Nick T

In DOS you can use editto create/modify text files, then execute them by typing python [yourfile]

在 DOS 中,您可以edit用来创建/修改文本文件,然后通过键入来执行它们python [yourfile]