How do I run a Python program?

Question:

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.

Asked By: Sergio Tapia

||

Answers:

You can just call

python /path/to/filename.py
Answered By: Peter

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.
Answered By: OscarRyz

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 SettingsGreggDesktoppyscripts

  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:Python26python.exe first.py

(Advanced users: instead of first.py, you could write out first.py’s full path of C:Documents and SettingsGreggDesktoppyscriptsfirst.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.

Answered By: Gregg Lind

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

Answered By: keneth

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.

Answered By: Transients

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.

Answered By: thegoldfish

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.

Answered By: Karen

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

Answered By: A.A Noman

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.

Answered By: Micheal O'Dwyer

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

Answered By: shiv
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.