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

时间:2020-12-15 09:24:44

I'm [relatively] new to 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比较陌生。我正在试图弄清楚如何在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。蝙蝠>文件。您可能想要在autoexecl . 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; (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.

这需要包括:C:\Python26;(或同等)。如果你把它放在前面,它将是第一个被看到的地方。你也可以把它加在最后,这可能比较明智。

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

然后重新启动提示符,并尝试输入“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?

有人知道怎么修复吗?

21 个解决方案

#1


209  

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

您需要向系统路径变量中添加C:\Python27,而不是一个名为“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.

查找系统路径环境变量,并将其附加到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环境变量列出了当给定命令的名称时,Windows(和cm .exe)将检查的所有位置,例如。“python”(它还使用PATHEXT变量作为可执行文件扩展的列表来尝试)。它在路径上找到的第一个具有该名称的可执行文件就是它启动的文件。

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,只需重新启动cmd的新实例。exe将具有更新的路径。您可以在命令提示符上键入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
  2. 计算机->系统属性(或Win+Break) ->高级系统设置
  3. Click the Environment variables... button (in the Advanced tab)
  4. 点击环境变量……按钮(在高级选项卡中)
  5. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  6. C:\Python27到最后(用你的Python版本代替)
  7. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
  8. 单击OK。注意,对路径的更改只反映在更改发生后打开的命令提示中。

#2


61  

Assuming you have Python2.7 installed

假设已经安装了Python2.7

  1. Goto the Start Menu

    转到开始菜单

  2. Right Click "Computer"

    右键点击“计算机”

  3. Select "Properties"

    选择“属性”

  4. A dialog should pop up with a link on the left called "Advanced system settings". Click it.

    对话框应该弹出,左边有一个链接,叫做“高级系统设置”。点击它。

  5. In the System Properties dialog, click the button called "Environment Variables".

    在“系统属性”对话框中,单击“环境变量”按钮。

  6. In the Environment Variables dialog look for "Path" under the System Variables window.

    在“环境变量”对话框中,在“系统变量”窗口中寻找“路径”。

  7. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

    在结尾加上“;C:\Python27”。分号是窗口上的路径分隔符。

  8. Click Ok and close the dialogs.

    单击Ok并关闭对话框。

  9. Now open up a new command prompt and type "python"

    现在打开一个新的命令提示符,输入“python”

It should work.

它应该工作。

#3


30  

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.

我花了一些精力在这里、web上、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启动程序,允许您输入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

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:\我的脚本\ applyres .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:\一些文件\一些lexicon.txt”。最简单的选择是移动文件或脚本,使它们位于相同的位置,但这可能会很混乱,所以我们假设它们将保持分离。

Making sure Windows can find the Python interpreter

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.

如果这不起作用,您需要在PATH环境变量中添加“;C:\Python32”(不带引号)之类的内容。有关说明,请参阅下面的PATHEXT。

Associating Python with .py and .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文件,选择Open With,选择Program,并检查“Always use…”这种关联提高了便利性,但并不是必须的——您可以在每次运行脚本时指定“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并与.py相关联之后,双击.py文件或直接将其作为命令输入就可以了。在这里,我们似乎是直接运行脚本——在“我的脚本”文件夹中的示例文件上运行脚本很好,也很简单。

D:\my scripts>ApplyRE.py lexicon-sample.txt -o
Running... Done.

Omitting the .py extension (editing 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变量,或者创建它,如果你是certan,它还不存在。关闭并重新打开命令提示符。现在应该可以省略.py(顺便说一下,这样做会引起ApplyRE。exe或ApplyRE。如果有蝙蝠的话,蝙蝠就会跑)。

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文件的话,这就不那么重要了),那么您需要将脚本的文件夹添加到系统路径中。(在PATHEXT旁边,你会看到一个路径变量;加“;D:我的脚本”,没有引号。通过这种方式,您可以针对当前位置的文件从其他位置运行脚本,如下所示:

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

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.

如果您是一个快速的打字员,或者不介意为每种情况创建一个批处理文件,您可以指定完整的路径(对于脚本或参数),而不是调整路径。

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)右键单击并创建一个快捷方式。右键单击用于编辑属性和将参数附加到目标的快捷方式。(b)创建一个批处理文件——一个纯文本文件,具有不同的名称,例如ApplyRErun.bat。这个选项可能更好,因为您可以要求它暂停,以便您可以看到输出。这是一个BAT文件的内容示例,从c:\一些文件写入并运行。

python "d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
pause

Advanced: appending to 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:\我的脚本添加到该变量,那么其他位置的其他Python脚本可以通过导入语句使用这些脚本。

#4


7  

You have to put the python path in the PATH variable.

必须将python路径放入path变量中。

In the System Variables section, you should have User Variables and System Variables. Search for the PATH variable and edit its value, adding at the end ;C:\python27.

在System Variables一节中,应该包含用户变量和系统变量。搜索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%添加您创建的变量。

#5


6  

Python comes with a script that takes care of setting up the windows path file for you.

Python附带了一个脚本,该脚本负责为您设置windows路径文件。

After installation, open command prompt

安装后,打开命令提示符。

cmd

cmd

Go to the directory you installed Python in

转到您安装Python的目录

cd C:\Python27

cd C:\ Python27

Run python and the win_add2path.py script in Tools\Scripts

运行python和win_add2path。py脚本工具\脚本

python.exe Tools\Scripts\win_add2path.py

python。exe工具\ \ win_add2path.py的脚本

Now you can use python as a command anywhere.

现在您可以在任何地方使用python作为命令。

#6


3  

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

不向系统变量添加任何变量。使用现有的“Path”系统变量,然后在后面添加一个分号,然后添加c:\Python27

#7


2  

first make sure u enter the path environmental variable

首先确保你输入路径环境变量

C:\ path %path%;C:\Python27 press Enter

C:\ path% path%;C:\Python27按回车键

C:\Python27>python file_name press Enter

C:\ Python27 > python file_name按回车

#8


2  

Just want to mention, when you do:

我只想说,当你这么做的时候:

cd C:\Python27
python Tools\Scripts\win_add2path.py

The PATH variable in "user variables for administrator" is changed.

“管理员用户变量”中的路径变量将被更改。

But you can also follow the others' answer to open:

但你也可以按照别人的回答打开:

System -> advanced system settings -> advanced -> Environment Variables,

系统->高级系统设置->高级->环境变量,

and modify/add the variable Path in "System Variables", add ;C:\Python27 at the end of it.

修改/添加“系统变量”中的变量路径,添加;C:\Python27最后。

#9


2  

  • Go to 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.

    在结尾加上“;C:\Python27”。分号是窗口上的路径分隔符。

  • Click Ok and close the dialogs.

    单击Ok并关闭对话框。

  • Now open up a new command prompt and type "python" or if it says error type "py" instead of "python"

    现在打开一个新的命令提示符,输入“python”或者如果它说错误类型“py”而不是“python”

#10


1  

press start button then type cmd. - Note you will need to run the command prompt as 'Adminstrator'.

按下开始按钮,然后输入cmd。-注意,您需要以“Adminstrator”方式运行命令提示符。

write setx -m path C:\Python27 then press enter.

写入setx -m路径C:\Python27然后按enter。

[here -m for giving accessing permission to all users and in Python27 27 is version 2.7]

[此处-m用于向所有用户授予访问权限,在Python27中,第27版是2.7版]

that's it,you are done.

就是这样,你做的。

#11


1  

So after 30 min of R&D i realized that after setup the PATH at environment variable

所以在30分钟的研发之后,我意识到在设置了环境变量的路径之后

i.e.

即。

" C:\Python/27; "

“C:\ Python / 27个;”

just restart

刚刚重启

now open cmd :

现在打开cmd:

C:> cd Python27 C:\ Python27> python.exe

C:> cd Python27 C:\ Python27> python.exe。

USE python.exe with extension

使用python。exe与扩展

alternative option is :

选择是:

if the software is installed properly directly run Python program, your command line screen will automatically appear without cmd.

如果软件安装正确,直接运行Python程序,您的命令行屏幕将在没有cmd的情况下自动显示。

Thanks.

谢谢。

#12


0  

You need to edit the environment variable named PATH, and add ;c:\python27 to the end of that. The semicolon separates one pathname from another (you will already have several things in your PATH).

您需要编辑名为PATH的环境变量,并在其末尾添加;c:\python27。分号将一个路径名与另一个路径名分开(您的路径中已经有几个东西)。

Alternately, you can just type

或者,你可以直接输入

c:\python27\python

at the command prompt without having to modify any environment variables at all.

在命令提示符下,无需修改任何环境变量。

#13


0  

On Windows you use C:\Python27\python.exe instead of python.

在Windows上使用C:\Python27\python。exe不是python。

If you add C:\Python27 to your path, you can shorten it to just python.exe, but you do not need to do this.

如果将C:\Python27添加到路径中,您可以将它缩短为python。exe,但你不需要这样做。

#14


0  

Modify the PATH variable too and append ;%python% otherwise the executable can not be found.

也修改PATH变量并追加;%python%否则无法找到可执行文件。

#15


0  

First install the Python into your windows by using this url and then add path variable as

首先使用此url将Python安装到windows中,然后添加path变量。

c:\python27

#16


0  

Use this PATH in Windows 7:

在Windows 7中使用此路径:

C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

#17


0  

I also found the same problem even though i've added the path in the environment variable. Finally, I put my "C:\Python27" in the FRONT part of the "PATH" in environment variable and after restarting the cmd, it works!!! I hope this can help.

我也发现了同样的问题,尽管我在环境变量中添加了路径。最后,我将我的“C:\Python27”放在环境变量的“路径”前面,在重新启动cmd之后,它就可以工作了!!!我希望这能有所帮助。

#18


0  

in powershell enter the following:

在powershell中输入如下:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

close and open the powershell and try again. this should solve your problem.

关闭并打开powershell并再次尝试。这应该能解决你的问题。

#19


0  

For Windows 10 & Python 3.5.1 users:

对于Windows 10和Python 3.5.1用户:

While installing Python on Windows 10, please don't forget to check the "Add to cmd prompt" option before hitting the "Install". This would help in easily access python from cmd.

在Windows 10上安装Python时,请不要忘记在点击“Install”之前检查“Add to cmd prompt”选项。这将有助于从cmd轻松访问python。

If the option was not checked, then please use Set Path in cmd to see if it is available as executables or not. If not, Navigate to Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> Select PATH from System Variables and Edit it. Then copy "C:\Python35\cmd" in the new line. After this please add .PY to PATHEXT in the same procedure.

如果没有选中该选项,那么请在cmd中使用Set Path查看它是否可用为可执行文件。如果没有,导航到启动>>控制面板>>系统和安全>>系统>>高级系统设置>>高级>0 >1环境变量。>>从系统变量中选择路径并对其进行编辑。然后在新行中复制“C:\Python35\cmd”。在此之后,请在相同的过程中向PATHEXT中添加. py。

Also please check if Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> User variables from Username >> PATH is containing these two lines - "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\Scripts\" & "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\". Else please add them manually.

也请检查是否启动>>控制面板>>系统和安全>>系统>>高级系统设置>>高级>0 >1环境变量>>路径下的>用户变量包含了这两行:“C:\用户名\应用程序\用户名\应用程序\本地\程序\Python\Python \Python35-32\脚本\ &“C:\用户用户名\应用程序\用户名\应用程序\本地\程序\Python\Python \Python35-32\程序”\ \ \ \ 32\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \程序\程序\程序否则请手动添加。

Ref: https://docs.python.org/3/using/windows.html

裁判:https://docs.python.org/3/using/windows.html

#20


0  

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”。分号是窗口上的路径分隔符。单击Ok并关闭对话框。现在打开一个新的命令提示符,输入“python”

If still the problem persists then type "py" instead of "python" in command prompt. might help!!!!

如果问题仍然存在,那么在命令提示符中输入“py”而不是“python”。可能帮助! ! ! !

#21


-3  

For windows 8, just type "py".

对于windows 8,只需输入“py”。

#1


209  

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

您需要向系统路径变量中添加C:\Python27,而不是一个名为“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.

查找系统路径环境变量,并将其附加到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环境变量列出了当给定命令的名称时,Windows(和cm .exe)将检查的所有位置,例如。“python”(它还使用PATHEXT变量作为可执行文件扩展的列表来尝试)。它在路径上找到的第一个具有该名称的可执行文件就是它启动的文件。

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,只需重新启动cmd的新实例。exe将具有更新的路径。您可以在命令提示符上键入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
  2. 计算机->系统属性(或Win+Break) ->高级系统设置
  3. Click the Environment variables... button (in the Advanced tab)
  4. 点击环境变量……按钮(在高级选项卡中)
  5. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  6. C:\Python27到最后(用你的Python版本代替)
  7. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
  8. 单击OK。注意,对路径的更改只反映在更改发生后打开的命令提示中。

#2


61  

Assuming you have Python2.7 installed

假设已经安装了Python2.7

  1. Goto the Start Menu

    转到开始菜单

  2. Right Click "Computer"

    右键点击“计算机”

  3. Select "Properties"

    选择“属性”

  4. A dialog should pop up with a link on the left called "Advanced system settings". Click it.

    对话框应该弹出,左边有一个链接,叫做“高级系统设置”。点击它。

  5. In the System Properties dialog, click the button called "Environment Variables".

    在“系统属性”对话框中,单击“环境变量”按钮。

  6. In the Environment Variables dialog look for "Path" under the System Variables window.

    在“环境变量”对话框中,在“系统变量”窗口中寻找“路径”。

  7. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

    在结尾加上“;C:\Python27”。分号是窗口上的路径分隔符。

  8. Click Ok and close the dialogs.

    单击Ok并关闭对话框。

  9. Now open up a new command prompt and type "python"

    现在打开一个新的命令提示符,输入“python”

It should work.

它应该工作。

#3


30  

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.

我花了一些精力在这里、web上、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启动程序,允许您输入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

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:\我的脚本\ applyres .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:\一些文件\一些lexicon.txt”。最简单的选择是移动文件或脚本,使它们位于相同的位置,但这可能会很混乱,所以我们假设它们将保持分离。

Making sure Windows can find the Python interpreter

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.

如果这不起作用,您需要在PATH环境变量中添加“;C:\Python32”(不带引号)之类的内容。有关说明,请参阅下面的PATHEXT。

Associating Python with .py and .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文件,选择Open With,选择Program,并检查“Always use…”这种关联提高了便利性,但并不是必须的——您可以在每次运行脚本时指定“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并与.py相关联之后,双击.py文件或直接将其作为命令输入就可以了。在这里,我们似乎是直接运行脚本——在“我的脚本”文件夹中的示例文件上运行脚本很好,也很简单。

D:\my scripts>ApplyRE.py lexicon-sample.txt -o
Running... Done.

Omitting the .py extension (editing 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变量,或者创建它,如果你是certan,它还不存在。关闭并重新打开命令提示符。现在应该可以省略.py(顺便说一下,这样做会引起ApplyRE。exe或ApplyRE。如果有蝙蝠的话,蝙蝠就会跑)。

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文件的话,这就不那么重要了),那么您需要将脚本的文件夹添加到系统路径中。(在PATHEXT旁边,你会看到一个路径变量;加“;D:我的脚本”,没有引号。通过这种方式,您可以针对当前位置的文件从其他位置运行脚本,如下所示:

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

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.

如果您是一个快速的打字员,或者不介意为每种情况创建一个批处理文件,您可以指定完整的路径(对于脚本或参数),而不是调整路径。

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)右键单击并创建一个快捷方式。右键单击用于编辑属性和将参数附加到目标的快捷方式。(b)创建一个批处理文件——一个纯文本文件,具有不同的名称,例如ApplyRErun.bat。这个选项可能更好,因为您可以要求它暂停,以便您可以看到输出。这是一个BAT文件的内容示例,从c:\一些文件写入并运行。

python "d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
pause

Advanced: appending to 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:\我的脚本添加到该变量,那么其他位置的其他Python脚本可以通过导入语句使用这些脚本。

#4


7  

You have to put the python path in the PATH variable.

必须将python路径放入path变量中。

In the System Variables section, you should have User Variables and System Variables. Search for the PATH variable and edit its value, adding at the end ;C:\python27.

在System Variables一节中,应该包含用户变量和系统变量。搜索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%添加您创建的变量。

#5


6  

Python comes with a script that takes care of setting up the windows path file for you.

Python附带了一个脚本,该脚本负责为您设置windows路径文件。

After installation, open command prompt

安装后,打开命令提示符。

cmd

cmd

Go to the directory you installed Python in

转到您安装Python的目录

cd C:\Python27

cd C:\ Python27

Run python and the win_add2path.py script in Tools\Scripts

运行python和win_add2path。py脚本工具\脚本

python.exe Tools\Scripts\win_add2path.py

python。exe工具\ \ win_add2path.py的脚本

Now you can use python as a command anywhere.

现在您可以在任何地方使用python作为命令。

#6


3  

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

不向系统变量添加任何变量。使用现有的“Path”系统变量,然后在后面添加一个分号,然后添加c:\Python27

#7


2  

first make sure u enter the path environmental variable

首先确保你输入路径环境变量

C:\ path %path%;C:\Python27 press Enter

C:\ path% path%;C:\Python27按回车键

C:\Python27>python file_name press Enter

C:\ Python27 > python file_name按回车

#8


2  

Just want to mention, when you do:

我只想说,当你这么做的时候:

cd C:\Python27
python Tools\Scripts\win_add2path.py

The PATH variable in "user variables for administrator" is changed.

“管理员用户变量”中的路径变量将被更改。

But you can also follow the others' answer to open:

但你也可以按照别人的回答打开:

System -> advanced system settings -> advanced -> Environment Variables,

系统->高级系统设置->高级->环境变量,

and modify/add the variable Path in "System Variables", add ;C:\Python27 at the end of it.

修改/添加“系统变量”中的变量路径,添加;C:\Python27最后。

#9


2  

  • Go to 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.

    在结尾加上“;C:\Python27”。分号是窗口上的路径分隔符。

  • Click Ok and close the dialogs.

    单击Ok并关闭对话框。

  • Now open up a new command prompt and type "python" or if it says error type "py" instead of "python"

    现在打开一个新的命令提示符,输入“python”或者如果它说错误类型“py”而不是“python”

#10


1  

press start button then type cmd. - Note you will need to run the command prompt as 'Adminstrator'.

按下开始按钮,然后输入cmd。-注意,您需要以“Adminstrator”方式运行命令提示符。

write setx -m path C:\Python27 then press enter.

写入setx -m路径C:\Python27然后按enter。

[here -m for giving accessing permission to all users and in Python27 27 is version 2.7]

[此处-m用于向所有用户授予访问权限,在Python27中,第27版是2.7版]

that's it,you are done.

就是这样,你做的。

#11


1  

So after 30 min of R&D i realized that after setup the PATH at environment variable

所以在30分钟的研发之后,我意识到在设置了环境变量的路径之后

i.e.

即。

" C:\Python/27; "

“C:\ Python / 27个;”

just restart

刚刚重启

now open cmd :

现在打开cmd:

C:> cd Python27 C:\ Python27> python.exe

C:> cd Python27 C:\ Python27> python.exe。

USE python.exe with extension

使用python。exe与扩展

alternative option is :

选择是:

if the software is installed properly directly run Python program, your command line screen will automatically appear without cmd.

如果软件安装正确,直接运行Python程序,您的命令行屏幕将在没有cmd的情况下自动显示。

Thanks.

谢谢。

#12


0  

You need to edit the environment variable named PATH, and add ;c:\python27 to the end of that. The semicolon separates one pathname from another (you will already have several things in your PATH).

您需要编辑名为PATH的环境变量,并在其末尾添加;c:\python27。分号将一个路径名与另一个路径名分开(您的路径中已经有几个东西)。

Alternately, you can just type

或者,你可以直接输入

c:\python27\python

at the command prompt without having to modify any environment variables at all.

在命令提示符下,无需修改任何环境变量。

#13


0  

On Windows you use C:\Python27\python.exe instead of python.

在Windows上使用C:\Python27\python。exe不是python。

If you add C:\Python27 to your path, you can shorten it to just python.exe, but you do not need to do this.

如果将C:\Python27添加到路径中,您可以将它缩短为python。exe,但你不需要这样做。

#14


0  

Modify the PATH variable too and append ;%python% otherwise the executable can not be found.

也修改PATH变量并追加;%python%否则无法找到可执行文件。

#15


0  

First install the Python into your windows by using this url and then add path variable as

首先使用此url将Python安装到windows中,然后添加path变量。

c:\python27

#16


0  

Use this PATH in Windows 7:

在Windows 7中使用此路径:

C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

#17


0  

I also found the same problem even though i've added the path in the environment variable. Finally, I put my "C:\Python27" in the FRONT part of the "PATH" in environment variable and after restarting the cmd, it works!!! I hope this can help.

我也发现了同样的问题,尽管我在环境变量中添加了路径。最后,我将我的“C:\Python27”放在环境变量的“路径”前面,在重新启动cmd之后,它就可以工作了!!!我希望这能有所帮助。

#18


0  

in powershell enter the following:

在powershell中输入如下:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

close and open the powershell and try again. this should solve your problem.

关闭并打开powershell并再次尝试。这应该能解决你的问题。

#19


0  

For Windows 10 & Python 3.5.1 users:

对于Windows 10和Python 3.5.1用户:

While installing Python on Windows 10, please don't forget to check the "Add to cmd prompt" option before hitting the "Install". This would help in easily access python from cmd.

在Windows 10上安装Python时,请不要忘记在点击“Install”之前检查“Add to cmd prompt”选项。这将有助于从cmd轻松访问python。

If the option was not checked, then please use Set Path in cmd to see if it is available as executables or not. If not, Navigate to Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> Select PATH from System Variables and Edit it. Then copy "C:\Python35\cmd" in the new line. After this please add .PY to PATHEXT in the same procedure.

如果没有选中该选项,那么请在cmd中使用Set Path查看它是否可用为可执行文件。如果没有,导航到启动>>控制面板>>系统和安全>>系统>>高级系统设置>>高级>0 >1环境变量。>>从系统变量中选择路径并对其进行编辑。然后在新行中复制“C:\Python35\cmd”。在此之后,请在相同的过程中向PATHEXT中添加. py。

Also please check if Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> User variables from Username >> PATH is containing these two lines - "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\Scripts\" & "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\". Else please add them manually.

也请检查是否启动>>控制面板>>系统和安全>>系统>>高级系统设置>>高级>0 >1环境变量>>路径下的>用户变量包含了这两行:“C:\用户名\应用程序\用户名\应用程序\本地\程序\Python\Python \Python35-32\脚本\ &“C:\用户用户名\应用程序\用户名\应用程序\本地\程序\Python\Python \Python35-32\程序”\ \ \ \ 32\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \程序\程序\程序否则请手动添加。

Ref: https://docs.python.org/3/using/windows.html

裁判:https://docs.python.org/3/using/windows.html

#20


0  

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”。分号是窗口上的路径分隔符。单击Ok并关闭对话框。现在打开一个新的命令提示符,输入“python”

If still the problem persists then type "py" instead of "python" in command prompt. might help!!!!

如果问题仍然存在,那么在命令提示符中输入“py”而不是“python”。可能帮助! ! ! !

#21


-3  

For windows 8, just type "py".

对于windows 8,只需输入“py”。