python - 如何在 Windows 7 的命令提示符中运行 Python 程序?

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

当我在命令提示符中输入“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 .

这有点帮助,但本教程是为 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:

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

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

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.

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

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

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

最佳答案

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

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

PATH 环境变量列出了 Windows(和 cmd.exe)在给定命令名称时将检查的所有位置,例如“python”(它还使用 PATHEXT 变量作为可执行文件扩展名列表来尝试)。它在 PATH 上找到的第一个具有该名称的可执行文件就是它启动的那个。

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


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

  1. 计算机 -> 系统属性(或 Win+Break)-> 高级系统设置
  2. 单击环境变量...按钮(在“高级”选项卡中)
  3. 编辑 PATH 并将 ;C:\Python27 附加到末尾(替换您的 Python 版本)
  4. 点击确定。请注意,对 PATH 的更改仅反射(reflect)在更改发生后打开的命令提示符中。

https://stackoverflow.com/questions/4621255/

相关文章:

python - 将 javadoc 用于 Python 文档

linux - 从 ps -ef |grep 关键字获取 pid

python - 何时在 Python 中使用 %r 而不是 %s?

python - 从函数中获取文档字符串

linux - 在 Bash 中,如何在文件的每一行之后添加一个字符串?

linux - 如何从脚本编辑/etc/sudoers?

python - 将 python 列表拆分为其他 "sublists"即较小的列表

python - 如何为每个子图添加标题

linux - grep 的基本表达式不支持\d 吗?

linux - 如何通过 * 将包括隐藏文件在内的所有文件移动到父目录中