如何在 Windows 7 的命令提示符中运行 Python 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4621255/
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
How do I run a Python program in the Command Prompt in Windows 7?
提问by Adam Seitz
I'm trying to figure out how to run Python programs with the Command Prompt on Windows 7. (I should have figured this out by now...)
我正在尝试弄清楚如何在 Windows 7 上使用命令提示符运行 Python 程序。(我现在应该已经弄清楚了......)
When I typed "python" into the command prompt, I got the following error:
当我在命令提示符中输入“python”时,出现以下错误:
'python' is not recognized as an internal or external command, operable program or batch file.
'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。
The first place I found when looking for help was this site: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows.
我在寻求帮助时首先找到的是这个站点:http: //docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows。
It was somewhat helpful, but the tutorial was written for Windows 2000 and older, so it was minimally helpful for my Windows 7 machine. I attempted the following:
这有点帮助,但该教程是为 Windows 2000 及更早版本编写的,因此它对我的 Windows 7 机器的帮助微乎其微。我尝试了以下操作:
For older versions of Windows the easiest way to do this is to edit the C:\AUTOEXEC.BAT >file. You would want to add a line like the following to AUTOEXEC.BAT:
对于旧版本的 Windows,最简单的方法是编辑 C:\AUTOEXEC.BAT > 文件。您可能希望在 AUTOEXEC.BAT 中添加如下一行:
This file did not exist on my machine (unless I'm mistaken).
这个文件在我的机器上不存在(除非我弄错了)。
Next, I tried this: (here: How do I run a Python program?)
接下来,我尝试了这个:(这里:如何运行 Python 程序?)
Putting Python In Your Path
Windows
In order to run programs, your operating system looks in various places, and tries to match the name of the program / command you typed with some programs along the way.
In windows:
control panel > system > advanced > |Environmental Variables| > system variables -> Path
this needs to include: C:\Python26; (or equivalent). If you put it at the front, it will be the first place looked. You can also add it at the end, which is possibly saner.
Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.
将 Python 放在您的路径中
视窗
为了运行程序,您的操作系统会在各个地方查找,并尝试将您键入的程序/命令的名称与一路上的某些程序匹配。
在窗口中:
控制面板 > 系统 > 高级 > |环境变量| > 系统变量 -> 路径
这需要包括:C:\Python26; (或同等学历)。如果你把它放在前面,它会是第一个被看到的地方。您也可以在最后添加它,这可能更明智。
然后重新启动提示,并尝试键入“python”。如果一切正常,您应该得到“>>>”提示。
This was relevant enough for Windows 7, and I made my way to the System Variables. I added a variable "python" with the value "C:\Python27"
这对于 Windows 7 来说已经足够相关了,我开始使用系统变量。我添加了一个变量“python”,值为“C:\Python27”
I continued to get the error, even after restarting my computer.
即使在重新启动计算机后,我仍然收到错误消息。
Anyone know how to fix this?
有人知道怎么修这个东西吗?
采纳答案by Cameron
You need to add C:\Python27to your system PATH variable, not a new variable named "python".
您需要添加C:\Python27到系统 PATH 变量,而不是名为“python”的新变量。
Find the system PATH environment variable, and append to it a ;(which is the delimiter) and the path to the directory containing python.exe (e.g. C:\Python27). See below for exact steps.
找到系统 PATH 环境变量,并在;其后附加 a (它是分隔符)和包含 python.exe 的目录的路径(例如C:\Python27)。具体步骤见下文。
The PATH environment variable lists all the locations that Windows (and cmd.exe) will check when given the name of a command, e.g. "python" (it also uses the PATHEXT variable for a list of executable file extensions to try). The first executable file it finds on the PATH with that name is the one it starts.
PATH 环境变量列出了cmd.exe在给定命令名称时Windows(和)将检查的所有位置,例如“python”(它还使用 PATHEXT 变量作为要尝试的可执行文件扩展名列表)。它在 PATH 中找到的第一个具有该名称的可执行文件就是它开始的那个。
Note that after changing this variable, there is no need to restart Windows, but only new instances of cmd.exewill have the updated PATH. You can type set PATHat the command prompt to see what the current value is.
请注意,更改此变量后,无需重新启动 Windows,但只有 的新实例cmd.exe才会具有更新的 PATH。您可以set PATH在命令提示符下键入以查看当前值。
Exact steps for adding Python to the path on Windows 7+:
在 Windows 7+ 上将 Python 添加到路径的确切步骤:
- Computer -> System Properties (or Win+Break) -> Advanced System Settings
- Click the
Environment variables...button (in the Advanced tab) - Edit PATH and append
;C:\Python27to the end (substitute your Python version) - Click OK. Note that changes to the PATH are only reflected in command prompts opened afterthe change took place.
- 计算机 -> 系统属性(或Win+Break) -> 高级系统设置
- 单击
Environment variables...按钮(在“高级”选项卡中) - 编辑 PATH 并附
;C:\Python27加到末尾(替换您的 Python 版本) - 单击确定。请注意,对 PATH 的更改仅反映在更改发生后打开的命令提示符中。
回答by Raphael Bossek
Modify the PATHvariable too and append ;%python%otherwise the executable can not be found.
也修改PATH变量并附加,;%python%否则无法找到可执行文件。
回答by Greg Hewgill
You need to edit the environment variable named PATH, and add ;c:\python27to the end of that. The semicolon separates one pathname from another (you will already have several things in your PATH).
您需要编辑名为 的环境变量PATH,并将其添加;c:\python27到其末尾。分号将一个路径名与另一个路径名分开(您的 中已经有几项内容PATH)。
Alternately, you can just type
或者,您可以输入
c:\python27\python
at the command prompt without having to modify any environment variables at all.
在命令提示符下,根本无需修改任何环境变量。
回答by Gerrat
You don't add any variables to the System Variables. You take the existing 'Path' system variable, and modify it by adding a semi-colon after, then c:\Python27
您不会向系统变量添加任何变量。您采用现有的“路径”系统变量,并通过在其后添加分号来修改它,然后是 c:\Python27
回答by Lennart Regebro
On Windows you use C:\Python27\python.exeinstead of python.
在 Windows 上,您使用C:\Python27\python.exe而不是python.
If you add C:\Python27to your path, you can shorten it to just python.exe, but you do not need to do this.
如果您添加C:\Python27到您的路径,您可以将其缩短为 just python.exe,但您不需要这样做。
回答by oscarcardoso
You have to put the python path in the PATHvariable.
您必须将python路径放在PATH变量中。
In the System Variables section, you should have User Variables and System Variables. Search for the PATHvariable and edit its value, adding at the end ;C:\python27.
在系统变量部分,您应该有用户变量和系统变量。搜索PATH变量并编辑其值,在末尾添加;C:\python27.
The ;is to tell the variable to add a new path to this value, and the rest, is just to tell which path that is.
该;是告诉变量到一个新的路径添加到该值,剩下的,就是告诉哪条路径即是。
On the other hand, you can use ;%python%to add the variable you created.
另一方面,您可以使用;%python%添加您创建的变量。
回答by nathan
Assuming you have Python2.7 installed
假设你已经安装了 Python2.7
Goto the Start Menu
Right Click "Computer"
Select "Properties"
A dialog should pop up with a link on the left called "Advanced system settings". Click it.
In the System Properties dialog, click the button called "Environment Variables".
In the Environment Variables dialog look for "Path" under the System Variables window.
Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.
Click Ok and close the dialogs.
Now open up a new command prompt and type "python"
进入开始菜单
右键单击“计算机”
选择“属性”
应弹出一个对话框,左侧有一个名为“高级系统设置”的链接。点击它。
在“系统属性”对话框中,单击名为“环境变量”的按钮。
在“环境变量”对话框中,在“系统变量”窗口下查找“路径”。
将 ";C:\Python27" 添加到它的末尾。分号是 Windows 上的路径分隔符。
单击确定并关闭对话框。
现在打开一个新的命令提示符并输入“python”
It should work.
它应该工作。
回答by fargath
回答by Jon Coombs
It has taken me some effort looking for answers here, on the web, and and in the Python documentation, and testing on my own, to finally get my Python scripts working smoothly on my Windows machines (WinXP and Win7). So, I just blogged about it and am pasting that below in case it's useful to others. Sorry it's long, and feel free to improve it; I'm no expert.
我花了一些精力在这里、网络和 Python 文档中寻找答案,并自己进行测试,最终让我的 Python 脚本在我的 Windows 机器(WinXP 和 Win7)上顺利运行。所以,我刚刚写了一篇关于它的博客,并将其粘贴在下面,以防对其他人有用。对不起,它很长,请随时改进它;我不是专家。
[UPDATE: Python 3.3 now includes the Python Launcher for Windows, which allows you to type py (rather than python) to invoke the default interpreter, or py -2, py -3, py -2.7, etc. It also supports shebang lines, allowing the script itself to specify. For versions prior to 3.3, the launcher is available as a separate download. http://docs.python.org/3/whatsnew/3.3.html]
[更新:Python 3.3 现在包含适用于 Windows 的 Python Launcher,它允许您输入 py(而不是 python)来调用默认解释器,或 py -2、py -3、py -2.7 等。它还支持 shebang 行,允许脚本本身指定。对于 3.3 之前的版本,可以单独下载启动器。 http://docs.python.org/3/whatsnew/3.3.html]
Running Python scripts conveniently under Windows
在 Windows 下方便地运行 Python 脚本
Maybe you're creating your own Python scripts, or maybe someone has given you one for doing something with your data files. Say you've acquired a Python script and have saved it to "D:\my scripts\ApplyRE.py". You want to run it conveniently by either double-clicking it or typing it into the command line from any location, with the option of passing parameters to it like this (-o means "overwrite the output file if it already exists"):
也许您正在创建自己的 Python 脚本,或者也许有人给了您一个用于处理数据文件的脚本。假设您获得了一个 Python 脚本并将其保存到“D:\my scripts\ApplyRE.py”。您希望通过双击它或从任何位置将其键入命令行来方便地运行它,并可以选择像这样向其传递参数(-o 表示“如果输出文件已经存在,则覆盖它”):
ApplyRE infile.txt outfile.txt -o
Say you also have a data file, "C:\some files\some lexicon.txt". The simplest option is to move the file or the script so they're in the same location, but that can get messy, so let's assume that they'll stay separate.
假设您还有一个数据文件,“C:\some files\some lexicon.txt”。最简单的选择是移动文件或脚本,使它们位于同一位置,但这可能会变得混乱,因此让我们假设它们将保持分开。
Making sure Windows can find the Python interpreter
确保 Windows 可以找到 Python 解释器
After installing Python, verify that typing python into a command prompt works (and then type exit() to get back out of the Python interpreter).
安装 Python 后,验证在命令提示符中键入 python 是否有效(然后键入 exit() 以退出 Python 解释器)。
C:\>python
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\>
If this doesn't work, you'll need to append something like ";C:\Python32" (without quotes) to the PATH environment variable. See PATHEXT below for instructions.
如果这不起作用,您需要将诸如“;C:\Python32”(不带引号)之类的内容附加到 PATH 环境变量中。有关说明,请参阅下面的 PATHEXT。
Associating Python with .py and .pyc
将 Python 与 .py 和 .pyc 相关联
Verify that double-clicking on ApplyRE.py runs it. (It should also have a Python logo as its icon and be labeled "Python File", by the way.) If this isn't already done, right-click on a .py file, choose Open With, Choose Program, and check "Always use..." This association improves convenience but isn't strictly necessary--you can specify "python" every time you want to run a script, like this:
验证双击 ApplyRE.py 运行它。(顺便说一下,它还应该有一个 Python 徽标作为其图标,并标记为“Python 文件”。)如果这还没有完成,请右键单击 .py 文件,选择打开方式,选择程序,然后检查“始终使用...” 这种关联提高了便利性,但并不是绝对必要的——每次要运行脚本时都可以指定“python”,如下所示:
D:\my scripts>python ApplyRE.py lexicon-sample.txt -o
Running... Done.
Here's a very specific variation, which is optional unless you need to specify a different version of the interpreter.
这是一个非常具体的变体,除非您需要指定不同版本的解释器,否则它是可选的。
D:\my scripts>c:\python32\python ApplyRE.py lexicon-sample.txt -o
Running... Done.
But that's a pain. Fortunately, once Python is installed, in the PATH, and associated with .py, then double-clicking a .py file or directly typing it as a command should work fine. Here, we seem to be running the script directly--it's nice and simple to run it on a sample file that's located in the "my scripts" folder along with the script.
但这是一种痛苦。幸运的是,一旦 Python 安装在 PATH 中并与 .py 相关联,然后双击 .py 文件或直接将其作为命令键入应该可以正常工作。在这里,我们似乎直接运行脚本——在位于“我的脚本”文件夹中的示例文件上运行它既好又简单。
D:\my scripts>ApplyRE.py lexicon-sample.txt -o
Running... Done.
Omitting the .py extension (editing PATHEXT)
省略 .py 扩展名(编辑 PATHEXT)
To further reduce typing, you can tell Windows that .py (and perhaps .pyc files) are executable. To do this, right-click Computer and choose Properties, Advanced, Environment Variables, System Variables. Append ";.PY;.PYC" (without quotes) to the existing PATHEXT variable, or else create it if you're certan it doesn't exist yet. Close and reopen the command prompt. You should now be able to omit the .py (FYI, doing so would cause ApplyRE.exe or ApplyRE.bat to run instead, if one existed).
为了进一步减少输入,您可以告诉 Windows .py(可能还有 .pyc 文件)是可执行的。为此,请右键单击“计算机”并选择“属性”、“高级”、“环境变量”、“系统变量”。将“;.PY;.PYC”(不带引号)附加到现有的 PATHEXT 变量,或者如果您确定它尚不存在,则创建它。关闭并重新打开命令提示符。您现在应该可以省略 .py(仅供参考,这样做会导致 ApplyRE.exe 或 ApplyRE.bat 运行,如果存在的话)。
D:\my scripts>ApplyRE lexicon-sample.txt -o
Running... Done.
Adding scripts to the system PATH
将脚本添加到系统路径
If you're going to use your scripts often from the command prompt (it's less important if doing so via using BAT files), then you'll want to add your scripts' folder to the system PATH. (Next to PATHEXT you should see a PATH variable; append ";D:\my scripts" to it, without quotes.) This way you can run a script from some other location against the files in current location, like this:
如果您要经常从命令提示符使用脚本(如果使用 BAT 文件这样做就不那么重要了),那么您需要将脚本的文件夹添加到系统 PATH。(在 PATHEXT 旁边,您应该看到一个 PATH 变量;将“;D:\my scripts”附加到它,不带引号。)这样您就可以从其他位置针对当前位置的文件运行脚本,如下所示:
C:\some files>ApplyRE "some lexicon.txt" "some lexicon OUT.txt" -o
Running... Done.
Success! That's pretty much all you need to do to streamline the command-line.
成功!这几乎就是您简化命令行所需要做的全部工作。
Running directly without tweaking the PATH
直接运行而不调整 PATH
If you're a fast typist or don't mind creating a batch file for each situation, you can specify full paths (for the script, or for the parameters) instead of tweaking PATH.
如果您是一个快速打字员或不介意为每种情况创建一个批处理文件,您可以指定完整路径(用于脚本或参数)而不是调整 PATH。
C:\some files>"d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
Running... Done.
C:\some files>d:
D:\>cd "my scripts"
D:\my scripts>ApplyRE.py "c:\some files\some lexicon.txt" "c:\some files\some lexicon OUT.txt" -o
Running... Done.
Creating shortcuts or batch files
创建快捷方式或批处理文件
If .py is associated with an installed Python, you can just double-click ApplyRE.py to run it, but the console may appear and disappear too quickly to read its output (or failure!). And to pass parameters, you'd need to first do one of the following. (a) Right-click and create a shortcut. Right-click the shortcut to edit properties and append parameters to Target. (b) Create a batch file--a plain text file with a distinct name such as ApplyRErun.bat. This option is probably better because you can ask it to pause so you can see the output. Here is a sample BAT file's contents, written to be located and run from c:\some files .
如果 .py 与已安装的 Python 相关联,您只需双击 ApplyRE.py 即可运行它,但控制台可能会出现和消失太快而无法读取其输出(或失败!)。要传递参数,您需要先执行以下操作之一。(a) 右键单击并创建快捷方式。右键单击快捷方式以编辑属性并将参数附加到 Target。(b) 创建一个批处理文件——一个具有不同名称的纯文本文件,例如 ApplyRErun.bat。此选项可能更好,因为您可以要求它暂停以便您可以看到输出。这是一个示例 BAT 文件的内容,写入后可定位并从 c:\some files 运行。
python "d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
pause
Advanced: appending to PYTHONPATH
高级:附加到 PYTHONPATH
This usually isn't necessary, but one other environment variable that may be relevant is PYTHONPATH. If we were to append d:\my scripts to that variable, then other Python scripts in other locations could make use of those via import statements.
这通常不是必需的,但另一个可能相关的环境变量是 PYTHONPATH。如果我们将 d:\my 脚本附加到该变量,那么其他位置的其他 Python 脚本可以通过 import 语句使用这些脚本。
回答by Jáissom Barth
Use this PATH in Windows 7:
在 Windows 7 中使用此路径:
C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

