如何在 Windows 7 上启动 Python 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5253358/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:23:32  来源:igfitidea点击:

How to launch a Python file on Windows 7?

pythonwindowswindows-7

提问by user618111

this is the first time I have used Python. I downloaded the file ActivePython-2.7.1.4-win32-x86 and installed it on my computer; I'm using Win7.

这是我第一次使用 Python。我下载了文件 ActivePython-2.7.1.4-win32-x86 并安装在我的电脑上;我用的是Win7。

So when I tried to run a python program, it appears and disappears very quickly. I don't have enough time to see anything on the screen. I just downloaded the file and double-cliked on it.

所以当我试图运行一个python程序时,它出现和消失的速度非常快。我没有足够的时间在屏幕上看到任何东西。我刚刚下载了该文件并双击了它。

How do I launch this file? I know that it is a long file for a first Python tutorial.

我如何启动这个文件?我知道这是第一个 Python 教程的长文件。

采纳答案by Vamana

Or run it from a batch file:

或者从批处理文件运行它:

myprog.py
pause

Has the advantage that you can specify a different version of Python too.

优点是您也可以指定不同版本的 Python。

回答by Katriel

Add the line

添加行

input()

to the end of the program, with the correct indentation. The issue is that after the data is printed to the console the program finishes, so the console goes away. inputtells the program to wait for input, so the console won't be closed when it finishes printing.

到程序末尾,缩进正确。问题是在将数据打印到控制台后程序完成了,所以控制台消失了。input告诉程序等待输入,所以当它完成打印时控制台不会关闭。

I hope you're not using that program to learn Python; it's pretty complicated!

我希望你不是在使用那个程序来学习 Python;这很复杂!

回答by Adrien Plisson

go to Start > All programs > Accessoriesand click on Command Prompt. then drag the python file from the explorer view into this command line and press Enter...

转到Start > All programs > Accessories并单击Command Prompt。然后将 python 文件从资源管理器视图拖到此命令行中,然后按Enter...

now you can watch the output of the script execution !

现在您可以观看脚本执行的输出了!

回答by ypercube??

run it from a command prompt:

从命令提示符运行它:

> python myscript.py

You can also start only the python interpreter from the command prompt (or by running python.exe) and then try some commands:

您还可以从命令提示符(或通过运行 python.exe)仅启动 python 解释器,然后尝试一些命令:

> python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
>>> a = 2
>>> b = 7
>>> print a+b
9
>>> 

回答by peter2108

Just a bit more on this.

再多说一点。

You have a script myscript.pyin a folder C:\myscripts. This is how to set up Windows 7 so that you can type > myscriptinto a CMD window and the script will run.

myscript.py在文件夹中有一个脚本C:\myscripts。这是设置 Windows 7 的方法,以便您可以> myscript在 CMD 窗口中键入内容并运行脚本。

1) Set your PATHvariable to include the Python Interpreter.

1) 将PATH变量设置为包含 Python 解释器。

Control Panel > System and Security > System > Advanced Settings > Environment Variables. You can set either the System Variables or the User Variables. Scroll down till you find PATH, select it, click Edit.The Path appears selected in a new dialog. I always copy it into Notepad to edit it though all you need do is add ;C:\Python27to the end of the list. Save this.

控制面板 > 系统和安全 > 系统 > 高级设置 > 环境变量。您可以设置系统变量或用户变量。向下滚动直到找到PATH,选择它,单击Edit。路径在新对话框中显示为选中状态。我总是将它复制到记事本中进行编辑,尽管您需要做的就是将其添加;C:\Python27到列表的末尾。保存这个。

2) Set your PATHvariable to include C:\myscripts

2)设置您的PATH变量以包括C:\myscripts

3) Set your PATHEXTvariable to include ;.PY. (This is the bit that saves you from typing myscript.py)

3) 将PATHEXT变量设置为 include ;.PY。(这是让您免于打字的一点myscript.py

This may now just work. Try opening a command window and typing myscript

这现在可能会起作用。尝试打开命令窗口并输入myscript

But it may not. Windows can still mess you about. I had installed and then uninstalled a Python package and when I typed myscriptWindows opened a box asking me which program to use. I browsed for C:\python27\python.exeand clicked that. Windows opened another command window ran the script and closed it before I could see what my script had done! To fix this when Windows opens its dialog select your Python and click the "Always do this" checkbox at the bottom.Then it doesn't open and close another window and things work as they should. Or they did for me.

但它可能不是。Windows 仍然会让您感到困扰。我已经安装然后卸载了一个 Python 包,当我输入myscriptWindows 时,会打开一个框,询问我要使用哪个程序。我浏览C:\python27\python.exe并点击了它。Windows 打开另一个命令窗口运行脚本并在我看到我的脚本做了什么之前关闭了它!要在 Windows 打开其对话框时解决此问题,选择您的 Python 并单击底部的“始终执行此操作”复选框。然后它不会打开和关闭另一个窗口,并且一切正常。或者他们为我做了。

Added: Above does not say how to pass arguments to your script. For this see answer Windows fails to pass arguments to python script

补充:上面没有说明如何将参数传递给你的脚本。为此,请参阅答案Windows 无法将参数传递给 python 脚本