Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

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

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.

(它有点帮助,但该教程是为Windows 2000及更早版本编写的,因此它对我的Windows 7机器的帮助微乎其微。)

I attempted the following:

(我尝试了以下方法:)

For older versions of Windows the easiest way to do this is to edit the C:\AUTOEXEC.BAT >file.

(对于旧版本的Windows,最简单的方法是编辑C:\ AUTOEXEC.BAT>文件。)

You would want to add a line like the following to 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

(把Python放在你的路上)

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:

(在Windows中:)

control panel > system > advanced > |Environmental Variables|

(控制面板>系统>高级> |环境变量|)

> system variables -> Path

(>系统变量 - >路径)

this needs to include: C:\Python26;

(这需要包括: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'.

(然后重新启动提示,并尝试键入“python”。)

If it all worked, you should get a ">>>" prompt.

(如果一切正常,你应该得到一个“>>>”提示符。)

This was relevant enough for Windows 7, and I made my way to the System Variables.

(这对于Windows 7来说已经足够了,我开始使用系统变量。)

I added a variable "python" with the value "C:\Python27"

(我添加了一个变量“python”,其值为“C:\ Python27”)

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

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

Anyone know how to fix this?

(有人知道怎么修这个东西吗?)

  ask by Adam Seitz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to add C:\Python27 to 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 ;

(找到系统PATH环境变量,并附加到它;)

(which is the delimiter) and the path to the directory containing python.exe (eg C:\Python27 ).

((这是分隔符)和包含python.exe的目录的路径(例如C:\Python27 )。)

See below for exact steps.

(请参阅下面的确切步骤。)

The PATH environment variable lists all the locations that Windows (and cmd.exe ) will check when given the name of a command, eg "python" (it also uses the PATHEXT variable for a list of executable file extensions to try).

(PATH环境变量列出了Windows(和cmd.exe )在给定命令名称时将检查的所有位置,例如“python”(它还使用PATHEXT变量来查找可执行文件扩展名列表)。)

The first executable file it finds on the PATH with that name is the one it starts.

(它在具有该名称的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.

(请注意,更改此变量后,无需重新启动Windows,但只有cmd.exe新实例将具有更新的PATH。)

You can type set PATH at the command prompt to see what the current value is.

(您可以在命令提示符下键入set PATH以查看当前值。)


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

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

  1. Computer -> System Properties (or Win+Break ) -> Advanced System Settings

    (计算机 - >系统属性(或Win + Break ) - >高级系统设置)

  2. Click the Environment variables... button (in the Advanced tab)

    (单击Environment variables...按钮(在“高级”选项卡中))

  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)

    (编辑PATH并追加;C:\Python27到最后(替换你的Python版本))

  4. Click OK.

    (单击确定。)

    Note that changes to the PATH are only reflected in command prompts opened after the change took place.

    (请注意,对PATH的更改仅反映在更改发生打开的命令提示中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...