如何在 Windows 7 上启动 Python 文件?
这是我第一次使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将该行添加
到程序末尾,并使用正确的缩进。问题是,在数据打印到控制台后,程序完成,因此控制台消失。
input
告诉程序等待输入,因此打印完成后控制台不会关闭。我希望您不是使用该程序来学习 Python;而是使用该程序来学习 Python。这很复杂!
Add the line
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!
转到
开始>;所有节目>附件
,然后单击命令提示符
。然后将 python 文件从资源管理器视图拖到此命令行中并按Enter
...现在您可以观看脚本执行的输出!
go to
Start > All programs > Accessories
and click onCommand Prompt
. then drag the python file from the explorer view into this command line and pressEnter
...now you can watch the output of the script execution !
从命令提示符运行它:
您还可以从命令提示符(或通过运行 python.exe)仅启动 python 解释器,然后尝试一些命令:
run it from a command prompt:
You can also start only the python interpreter from the command prompt (or by running python.exe) and then try some commands:
或者从批处理文件运行它:
优点是您也可以指定不同版本的 Python。
Or run it from a batch file:
Has the advantage that you can specify a different version of Python too.
关于这一点再多说一点。
您在文件夹
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 folderC:\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, clickEdit
.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 includeC:\myscripts
3) Set your
PATHEXT
variable to include;.PY
. (This is the bit that saves you from typingmyscript.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 forC:\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