如何运行Python程序?

问题:如何运行Python程序?

所以我有点像Python,但是我遇到了问题……运行它。大声笑

我现在正在使用IDLE,但是它没有任何用处,因为您一次只能运行几行。

我还使用Komodo Edit创建实际的.py文件。

我的问题是,如何运行.py文件来测试实际程序?

我正在使用Windows 7和Komodo Edit 5作为我的IDE。在Komodo中按F5根本不起作用。

So I’m starting like Python a bit, but I’m having trouble erm…running it. Lol

I’m using IDLE for now, but its no use whatsoever because you can only run a couple of lines at a time.

I’m also using Komodo Edit to create the actual .py files.

My question is, how can I run the .py files to test out the actual program?

I’m using Windows 7, and Komodo Edit 5 as my IDE. Pressing F5 in Komodo doesn’t do anythin at all.


回答 0

我很高兴你问!我只是在Wikibook中解释这件事(显然是不完整的)。我们正在与Python新手合作,并且必须完全按照您的要求提供一些帮助!

Windows中的命令行Python:

  1. 使用编辑器中的“保存”或“另存为”将python代码文件保存在某处。让我们在某些文件夹中将其称为“ first.py”,例如您在桌面上创建的“ pyscripts”。

  2. 打开提示(Windows’cmd’shell,它是计算机的文本界面):

    开始>运行>“ cmd”(在小框中)。好。

  3. 使用命令“ cd”(更改目录)和“ dir”(显示目录中的文件,以验证您的头)导航到python文件所在的位置。对于我们的示例,

    > CD C:\ Documents and Settings \ Gregg \ Desktop \ pyscripts

  4. 尝试:

    > python first.py

如果收到此消息:

无法将“ python”识别为内部或外部命令,可操作程序或批处理文件。

然后是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中:

控制面板>系统>高级> |环境变量| >系统变量->路径

这需要包括: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:

  1. 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.

  2. Open a prompt (a Windows ‘cmd’ shell that is a text interface into the computer):

    start > run > “cmd” (in the little box). OK.

  3. 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

  4. try:

    > python first.py

If you get this message:

‘python’ is not recognized as an internal or external command, operable program or batch file.

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.


回答 1

你可以打电话

python /path/to/filename.py

You can just call

python /path/to/filename.py

回答 2

在IDLE中按F5

您可以使用IDLE打开您的.py文件,然后按F5键运行它。

您可以使用其他编辑器(如您所说的Komodo)打开同一文件,然后保存并再次按F5。F5与IDLE一起使用(即使使用其他工具完成编辑)。

如果您想根据本文直接从Komodo运行它:在Komodo Edit中执行Python代码,您必须:

  1. 转到工具箱->添加->新命令…
  2. 在顶部字段中输入名称“运行Python文件”
  3. 在“命令”字段中输入以下文本:

    %(python)%F 3.a可选单击“键绑定”选项卡,然后为该命令分配一个键盘命令

  4. 单击确定。

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:

  1. go to Toolbox -> Add -> New Command…
  2. in the top field enter the name ‘Run Python file’
  3. 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

  4. click Ok.

回答 3

Python本身带有一个编辑器,您可以从“ IDLE文件”>“新建文件”菜单选项进行访问。

将代码写入该文件,将其另存为[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.


回答 4

如果您不想通话filename.py,可以将其添加.PY到PATHEXT中,那样您就可以调用filename

if you dont want call filename.py you can add .PY to the PATHEXT, that way you will just call filename


回答 5

如果这对任何人都有用,则“ 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.


回答 6

我刚才所做的是,通过双击打开一个简单的python脚本。我刚刚将一个批处理文件添加到包含脚本的目录中:

@echo off
python exercise.py
pause>nul

(我的系统路径上有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:

@echo off
python exercise.py
pause>nul

(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.


回答 7

导航您的文件位置,只需按Shift按钮,然后单击文件名。单击选项卡,Open command window here然后在命令提示符下写入python file_name.py

Navigate your file location just press Shift button and click file name. Click tab Open command window here and write in your command prompt python file_name.py


回答 8

如果要运行#’。py’文件,只需在代码中写入print()即可实际看到它被打印出来。与python IDLE不同,您需要使用print()命令指定要打印的内容。例如。

import os
os.getcwd()
a=[1,2,3,4,5]
name= 'Python'
# Use print() function
print(a)
print(name)

输出[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.

import os
os.getcwd()
a=[1,2,3,4,5]
name= 'Python'
# Use print() function
print(a)
print(name)

OUTPUT [1, 2, 3, 4, 5] Python


回答 9

我已经尝试了上面列出的许多命令,但是即使将路径设置为包括我安装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 to py -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.