如何在 Windows 7 的命令提示符中运行 Python 程序? [英] How do I run a Python program in the Command Prompt in Windows 7?

查看:82
本文介绍了如何在 Windows 7 的命令提示符中运行 Python 程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在 Windows 7 上使用命令提示符运行 Python 程序.(我现在应该已经弄清楚了...)

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...)

当我输入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.

我在寻求帮助时首先找到的网站是:http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows.

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.

这有点帮助,但该教程是为 Windows 2000 及更早版本编写的,因此它对我的 Windows 7 机器的帮助微乎其微.我尝试了以下操作:

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,最简单的方法是编辑 C:\AUTOEXEC.BAT >文件.您可能希望在 AUTOEXEC.BAT 中添加如下一行:

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:

这个文件在我的机器上不存在(除非我弄错了).

This file did not exist on my machine (unless I'm mistaken).

接下来,我尝试了这个:(这里:How do I run a Python程序?)

Next, I tried this: (here: How do I run a Python program?)

将 Python 放在您的路径中

Putting Python In Your Path

视窗

为了运行程序,您的操作系统会在各个地方查找,并尝试将您键入的程序/命令的名称与一路上的某些程序匹配.

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.

在窗口中:

控制面板 >系统>高级 >|环境变量|>系统变量 ->路径

control panel > system > advanced > |Environmental Variables| > system variables -> Path

这需要包括:C:\Python26;(或同等学历).如果你把它放在前面,它会是第一个被看到的地方.也可以在最后加上,这样可能更理智.

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.

然后重新启动提示,并尝试输入python".如果一切正常,你应该得到一个>>>"提示.

Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.

这对于 Windows 7 来说已经足够相关了,我开始使用系统变量.我添加了一个变量python"值为C:\Python27"

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"

即使在重新启动计算机后,我仍然收到错误消息.

I continued to get the error, even after restarting my computer.

有人知道如何解决这个问题吗?

Anyone know how to fix this?

推荐答案

您需要将 C:\Python27 添加到您的系统 PATH 变量中,而不是一个名为python"的新变量.

You need to add C:\Python27 to your system PATH variable, not a new variable named "python".

找到系统PATH环境变量,并在其后面附加一个;(它是分隔符)和包含python.exe的目录的路径(例如C:\Python27代码>).具体步骤见下文.

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 环境变量列出了在给定命令名称时 Windows(和 cmd.exe)将检查的所有位置,例如python"(它还使用 PATHEXT 变量作为要尝试的可执行文件扩展名列表).它在 PATH 中找到的第一个具有该名称的可执行文件就是它开始的那个.

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.

请注意,更改此变量后,无需重新启动 Windows,但只有 cmd.exe 的新实例才会具有更新的 PATH.您可以在命令提示符下键入 set PATH 以查看当前值.

Note that after changing this variable, there is no need to restart Windows, but only new instances of cmd.exe will have the updated PATH. You can type set PATH at the command prompt to see what the current value is.

在 Windows 7+ 上将 Python 添加到路径的确切步骤:

Exact steps for adding Python to the path on Windows 7+:

  1. 计算机 -> 系统属性(或 Win+Break)-> 高级系统设置
  2. 单击环境变量... 按钮(在高级"选项卡中)
  3. 编辑 PATH 并将 ;C:\Python27 附加到末尾(替换您的 Python 版本)
  4. 单击确定".请注意,对 PATH 的更改仅反映在更改发生后打开的命令提示符中.
  1. Computer -> System Properties (or Win+Break) -> Advanced System Settings
  2. Click the Environment variables... button (in the Advanced tab)
  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  4. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.

这篇关于如何在 Windows 7 的命令提示符中运行 Python 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆