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

发布于 2024-10-21 03:46:41 字数 207 浏览 3 评论 0原文

这是我第一次使用Python。 我下载了文件 ActivePython-2.7.1.4-win32-x86 并将其安装到我的计算机上;我用的是Win7。

因此,当我尝试运行 python 程序时,它出现和消失的速度非常快。我没有足够的时间看屏幕上的任何内容。我刚刚下载了该文件并双击它。

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

楠木可依 2024-10-28 03:46:41

将该行添加

input()

到程序末尾,并使用正确的缩进。问题是,在数据打印到控制台后,程序完成,因此控制台消失。 input 告诉程序等待输入,因此打印完成后控制台不会关闭。

我希望您不是使用该程序来学习 Python;而是使用该程序来学习 Python。这很复杂!

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. input tells the program to wait for input, so the console won't be closed when it finishes printing.

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

oО清风挽发oО 2024-10-28 03:46:41

转到开始>;所有节目>附件,然后单击命令提示符。然后将 python 文件从资源管理器视图拖到此命令行中并按 Enter...

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

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

now you can watch the output of the script execution !

多彩岁月 2024-10-28 03:46:41

从命令提示符运行它:

> python myscript.py

您还可以从命令提示符(或通过运行 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
>>> 

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
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
>>> 
空名 2024-10-28 03:46:41

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

myprog.py
pause

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

Or run it from a batch file:

myprog.py
pause

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

柠檬心 2024-10-28 03:46:41

关于这一点再多说一点。

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

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

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

2) 设置 PATH 变量以包含 C:\myscripts

3) 设置 PATHEXT 变量以包含 ;.PY。 (这可以让您免于输入 myscript.py

这现在可能可以工作了。尝试打开命令窗口并输入 myscript

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

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

Just a bit more on this.

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

1) Set your PATH variable to include the Python Interpreter.

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:\Python27 to the end of the list. Save this.

2) Set your PATH variable to include C:\myscripts

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

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

But it may not. Windows can still mess you about. I had installed and then uninstalled a Python package and when I typed myscript Windows opened a box asking me which program to use. I browsed for C:\python27\python.exe and 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文