如何运行 Python 程序?
我使用 Komodo Edit 5 编写了一些 .py 文件。我的 IDE 窗口如下所示:
如何实际运行 .py 文件来测试程序?我尝试按 F5 但似乎没有任何反应。
我也尝试过使用 IDLE,但似乎一次只能运行几行。
I used Komodo Edit 5 to write some .py files. My IDE window looks like this:
How do I actually run the .py file to test the program? I tried pressing F5 but it didn't appear to do anything.
I also tried using IDLE, but it seems like you can only run a couple of lines at a time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我很高兴你问了!我只是在在我们的维基百科中解释这件事(这显然是不完整的)。我们正在与 Python 新手合作,并且必须帮助一些人解决您所要求的问题!
Windows 中的命令行 Python:
使用编辑器中的“保存”或“另存为”将 Python 代码文件保存在某处。让我们在某个文件夹中将其称为“first.py”,例如您在桌面上创建的“pyscripts”。
打开提示符(Windows“cmd”外壳,是计算机的文本界面):
开始>运行> “cmd”(在小框中)。好的。
使用命令“cd”(更改目录)和“dir”(显示目录中的文件,以验证您的头)导航到 python 文件所在的位置。对于我们的示例,类似于,
> cd C:\Documents and Settings\Gregg\Desktop\pyscripts
尝试:
> python first.py
如果您收到此消息:
那么 python (可以将 Python 翻译成“计算机指令”的解释器程序)不在您的路径上(请参阅下面将 Python 放入您的路径中)。然后尝试这样调用它(假设Python2.6,安装在通常的位置):
> C:\Python26\python.exe first.py
(高级用户:您可以写出first.py 的 C:\Documents and Settings\Gregg\Desktop\pyscripts\first.py 的完整路径,而不是first.py)
将 Python 放入您的路径
Windows
为了运行程序,您的操作系统会查找各个位置,
并尝试将您键入的程序/命令的名称与某些内容相匹配
沿途的节目。
在 Windows 中:
控制面板 >系统>高级> |环境变量| >系统变量->路径
需要包含:C:\Python26; (或同等内容)。如果你把它放在前面,
这将是第一个看到的地方。您也可以将其添加在最后,这可能更明智。
然后重新启动提示符,并尝试输入“python”。如果一切顺利,你应该
得到一个“>>”迅速的。
I'm very glad you asked! I was just working on explaining this very thing in our wikibook (which is obviously incomplete). We're working with Python novices, and had to help a few through exactly what you're asking!
Command-line Python in Windows:
Save your python code file somewhere, using "Save" or "Save as" in your editor. Lets call it 'first.py' in some folder, like "pyscripts" that you make on your Desktop.
Open a prompt (a Windows 'cmd' shell that is a text interface into the computer):
start > run > "cmd" (in the little box). OK.
Navigate to where your python file is, using the commands 'cd' (change directory) and 'dir' (to show files in the directory, to verify your head). For our example something like,
> cd C:\Documents and Settings\Gregg\Desktop\pyscripts
try:
> python first.py
If you get this message:
then python (the interpreter program that can translate Python into 'computer instructions') isn't on your path (see Putting Python in Your Path below). Then try calling it like this (assuming Python2.6, installed in the usual location):
> C:\Python26\python.exe first.py
(Advanced users: instead of first.py, you could write out first.py's full path of C:\Documents and Settings\Gregg\Desktop\pyscripts\first.py)
Putting Python In Your Path
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:
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.
Then restart your prompt, and try typing 'python'. If it all worked, you should
get a ">>>" prompt.
你可以打电话
You can just call
在 IDLE 中按 F5
您可以使用 IDLE 打开 .py 文件,然后按 F5 运行它。
您可以使用其他编辑器(如您所说的 Komodo)打开同一文件,保存并再次按 F5; F5 可与 IDLE 配合使用(即使使用其他工具完成编辑)。
如果您想根据本文直接从 Komodo 运行它: 在 Komodo Edit 中执行 Python 代码,您必须:
在“命令”字段中输入以下文本:
%(蟒蛇)%F
3.a(可选)单击“按键绑定”选项卡并为此命令分配一个按键命令
,
In IDLE press F5
You can open your .py file with IDLE and press F5 to run it.
You can open that same file with other editor ( like Komodo as you said ) save it and press F5 again; F5 works with IDLE ( even when the editing is done with another tool ).
If you want to run it directly from Komodo according to this article: Executing Python Code Within Komodo Edit you have to:
in the 'Command' field enter this text:
%(python) %F
3.a optionall click on the 'Key Binding' tab and assign a key command to this command
Python 本身带有一个编辑器,您可以从 IDLE File > 访问它。新的文件菜单选项。
在该文件中编写代码,将其另存为 [filename].py,然后(在同一文件编辑器窗口中)按 F5 执行您在 IDLE Shell 窗口中创建的代码。
注意:到目前为止,这对我来说是最简单、最直接的方法。
Python itself comes with an editor that you can access from the IDLE File > New File menu option.
Write the code in that file, save it as [filename].py and then (in that same file editor window) press F5 to execute the code you created in the IDLE Shell window.
Note: it's just been the easiest and most straightforward way for me so far.
如果您不想调用
filename.py
您可以将.PY
添加到 PATHEXT,这样您就只需调用filename
if you dont want call
filename.py
you can add.PY
to the PATHEXT, that way you will just callfilename
如果这对任何人有帮助,“python [filename].py”或“python.exe [filename.py]”都不适合我,但“start python [filename].py”对我有用。如果其他人在使用前两个命令时遇到问题,请尝试后一个命令。
If this helps anyone, neither "python [filename].py" or "python.exe [filename.py]" worked for me, but "start python [filename].py" did. If anyone else is experiencing issues with the former two commands, try the latter one.
我刚刚做了什么,通过双击打开一个简单的 python 脚本。我刚刚在包含脚本的目录中添加了一个批处理文件:(
我的系统路径上有 python 可执行文件。如果没有,当然需要包含其完整路径。)
然后我只需双击该批处理文件即可运行脚本。第三行使 cmd 窗口不会在脚本结束后立即关闭,以便您可以看到结果。 :) 完成后,只需关闭命令窗口即可。
What I just did, to open a simple python script by double clicking. I just added a batch file to the directory containing the script:
(I have the python executable on my system path. If not one would need include its complete path of course.)
Then I just can double click on the batch file to run the script. The third line keeps the cmd window from being dismissed as soon as the script ends, so you can see the results. :) When you're done just close the command window.
只需按
Shift
按钮并单击文件名即可导航文件位置。单击选项卡在此处打开命令窗口
并在命令提示符中写入python file_name.py
Navigate your file location just press
Shift
button and click file name. Click tabOpen command window here
and write in your command promptpython file_name.py
如果你想运行#'.py'文件
只需在代码中编写 print() 即可实际看到它被打印出来。
与 python IDLE 不同,您需要使用 print() 命令指定要打印的内容。
例如。
输出
[1,2,3,4,5]
Python
If you want to run the #'.py' file
just write in print() in your code to actually see it get printed.
Unlike python IDLE, you need to specify what you want to print using print() command.
For eg.
OUTPUT
[1, 2, 3, 4, 5]
Python
我已经尝试了上面列出的许多命令,但是没有一个起作用,即使在将路径设置为包含安装 Python 的目录之后也是如此。
命令 py -3 file.py 始终适合我,如果我想运行 Python 2 代码,只要 Python 2 在我的路径中,只需将命令更改为 py - 2 file.py 完美运行。
我使用的是 Windows,所以我不太确定这个命令是否适用于 Linux 或 Mac,但值得一试。
I have tried many of the commands listed above, however none worked, even after setting my path to include the directory where I installed Python.
The command
py -3 file.py
always works for me, and if I want to run Python 2 code, as long as Python 2 is in my path, just changing the command topy -2 file.py
works perfectly.I am using Windows, so I'm not too sure if this command will work on Linux, or Mac, but it's worth a try.