我如何运行Python程序?

时间:2020-12-21 20:41:07

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

所以我开始像Python一样,但我遇到麻烦...运行它。大声笑

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

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

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

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

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

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

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

我正在使用Windows 7和Komodo Edit 5作为我的IDE。在科莫多按F5并没有做任何事情。

我如何运行Python程序?

8 个解决方案

#1


112  

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!

你问我很高兴!我正在努力在我们的wikibook中解释这个问题(这显然是不完整的)。我们正在与Python新手合作,并且必须通过您正在询问的内容帮助我们!

Command-line Python in Windows:

Windows中的命令行Python:

  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.

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

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

    打开提示(Windows'cmd'shell是计算机的文本界面):

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

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

  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'(更改目录)和'dir'(显示目录中的文件,验证您的头部)导航到您的python文件所在的位置。对于我们的例子,像,

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

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

  4. try:

    尝试:

    > python first.py

    > python first.py

If you get this message:

如果你收到这条消息:

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

'python'不被识别为内部或外部命令,可操作程序或批处理文件。

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):

然后python(可以将Python翻译成'计算机指令'的解释程序)不在您的路径上(请参阅下面的将Python放入您的路径中)。然后尝试这样调用它(假设Python2.6,安装在通常的位置):

> C:\Python26\python.exe first.py

> 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)

(高级用户:而不是first.py,你可以写出first.py的C:\ Documents and Settings \ Gregg \ Desktop \ pyscripts \ first.py的完整路径)

Putting Python In Your Path

把Python放在你的路上

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:

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

这需要包括:C:\ Python26; (或同等学历)。如果你把它放在前面,那将是第一个看的地方。您也可以在最后添加它,这可能更安全。

Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.

然后重新启动提示,并尝试键入“python”。如果一切正常,你应该得到一个“>>>”提示。

#2


28  

You can just call

你可以打电话

python /path/to/filename.py

#3


14  

In IDLE press F5

在IDLE按F5

You can open your .py file with IDLE and press F5 to run it.

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

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

您可以使用其他编辑器(如您所说的Komodo)打开同一个文件保存并再次按F5; F5适用于IDLE(即使使用其他工具进行编辑)。

If you want to run it directly from Komodo according to this article: Executing Python Code Within Komodo Edit you have to:

如果你想根据这篇文章直接从Komodo运行它:在Komodo Edit中执行Python代码你必须:

  1. go to Toolbox -> Add -> New Command...
  2. 转到工具箱 - >添加 - >新命令...
  3. in the top field enter the name 'Run Python file'
  4. 在顶部字段中输入名称“运行Python文件”
  5. 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)%F 3.a选项单击“键绑定”选项卡并为此命令分配键命令

  6. click Ok.
  7. 单击确定。

#4


3  

Python itself comes with an editor that you can access from the IDLE File > New File menu option.

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

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.

将代码写入该文件,将其另存为[filename] .py然后(在同一文件编辑器窗口中)按F5执行您在IDLE Shell窗口中创建的代码。

Note: it's just been the easiest and most straightforward way for me so far.

注意:到目前为止,这对我来说只是最简单,最直接的方式。

#5


0  

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

如果你不想调用filename.py你可以将.PY添加到PATHEXT,这样你只需要调用filename

#6


0  

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 [filename] .py”或“python.exe [filename.py]”都不适合我,但是“启动python [filename] .py”就可以了。如果其他人遇到前两个命令的问题,请尝试后一个命令。

#7


0  

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:

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

@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.)

(我的系统路径上有python可执行文件。如果不是,那么当然不需要包含它的完整路径。)

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.

然后我可以双击批处理文件来运行脚本。第三行会在脚本结束后立即关闭cmd窗口,以便您可以看到结果。 :)当你完成后关闭命令窗口。

#8


0  

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

导航文件位置只需按Shift键并单击文件名即可。单击选项卡在此处打开命令窗口并在命令提示符下写入python file_name.py

#1


112  

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!

你问我很高兴!我正在努力在我们的wikibook中解释这个问题(这显然是不完整的)。我们正在与Python新手合作,并且必须通过您正在询问的内容帮助我们!

Command-line Python in Windows:

Windows中的命令行Python:

  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.

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

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

    打开提示(Windows'cmd'shell是计算机的文本界面):

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

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

  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'(更改目录)和'dir'(显示目录中的文件,验证您的头部)导航到您的python文件所在的位置。对于我们的例子,像,

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

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

  4. try:

    尝试:

    > python first.py

    > python first.py

If you get this message:

如果你收到这条消息:

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

'python'不被识别为内部或外部命令,可操作程序或批处理文件。

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):

然后python(可以将Python翻译成'计算机指令'的解释程序)不在您的路径上(请参阅下面的将Python放入您的路径中)。然后尝试这样调用它(假设Python2.6,安装在通常的位置):

> C:\Python26\python.exe first.py

> 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)

(高级用户:而不是first.py,你可以写出first.py的C:\ Documents and Settings \ Gregg \ Desktop \ pyscripts \ first.py的完整路径)

Putting Python In Your Path

把Python放在你的路上

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:

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

这需要包括:C:\ Python26; (或同等学历)。如果你把它放在前面,那将是第一个看的地方。您也可以在最后添加它,这可能更安全。

Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.

然后重新启动提示,并尝试键入“python”。如果一切正常,你应该得到一个“>>>”提示。

#2


28  

You can just call

你可以打电话

python /path/to/filename.py

#3


14  

In IDLE press F5

在IDLE按F5

You can open your .py file with IDLE and press F5 to run it.

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

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

您可以使用其他编辑器(如您所说的Komodo)打开同一个文件保存并再次按F5; F5适用于IDLE(即使使用其他工具进行编辑)。

If you want to run it directly from Komodo according to this article: Executing Python Code Within Komodo Edit you have to:

如果你想根据这篇文章直接从Komodo运行它:在Komodo Edit中执行Python代码你必须:

  1. go to Toolbox -> Add -> New Command...
  2. 转到工具箱 - >添加 - >新命令...
  3. in the top field enter the name 'Run Python file'
  4. 在顶部字段中输入名称“运行Python文件”
  5. 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)%F 3.a选项单击“键绑定”选项卡并为此命令分配键命令

  6. click Ok.
  7. 单击确定。

#4


3  

Python itself comes with an editor that you can access from the IDLE File > New File menu option.

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

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.

将代码写入该文件,将其另存为[filename] .py然后(在同一文件编辑器窗口中)按F5执行您在IDLE Shell窗口中创建的代码。

Note: it's just been the easiest and most straightforward way for me so far.

注意:到目前为止,这对我来说只是最简单,最直接的方式。

#5


0  

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

如果你不想调用filename.py你可以将.PY添加到PATHEXT,这样你只需要调用filename

#6


0  

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 [filename] .py”或“python.exe [filename.py]”都不适合我,但是“启动python [filename] .py”就可以了。如果其他人遇到前两个命令的问题,请尝试后一个命令。

#7


0  

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:

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

@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.)

(我的系统路径上有python可执行文件。如果不是,那么当然不需要包含它的完整路径。)

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.

然后我可以双击批处理文件来运行脚本。第三行会在脚本结束后立即关闭cmd窗口,以便您可以看到结果。 :)当你完成后关闭命令窗口。

#8


0  

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

导航文件位置只需按Shift键并单击文件名即可。单击选项卡在此处打开命令窗口并在命令提示符下写入python file_name.py