105

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254

10 Answers10

139

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.

Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
  • Sometimes if python ver. 3 has been installed you may have to type "python3" note that the ".exe" extension is not always required. Also it doesn't make any difference whether Windows is set up to use file extensions or not, that is just for display purposes. – QuentinUK Dec 13 '16 at 13:18
  • @QuentinUK In Windows we don't need to write python3, it's required in Linux or Mac – Vinay Yadav May 12 '21 at 03:10
32

You can just call

python /path/to/filename.py
Peter
  • 127,331
  • 53
  • 180
  • 211
  • 6
    Erm...what exactly do you mean by call? – Sergio Tapia Oct 05 '09 at 21:44
  • Open a terminal, and type this in. This is for mac and linux, or for windows on cygwin. Perhaps others can answer for non-cygwin windows installations. – Peter Oct 05 '09 at 21:46
  • This will work on Windows also (except possibly for the forward slashes), but only if python is in the path. Getting it into the path takes more explanation than is available in this comment box. – Michael Myers Oct 05 '09 at 21:51
  • I think Papuccino1 problems lay a little bit earlier that that. I remember having that feel of not knowing where to go when you just have a new technology in your hands ( I still feel it from time to time with Java :P ) – OscarRyz Oct 05 '09 at 21:56
  • 1
    by default on windows you could just do `filename.py` – SilentGhost Oct 05 '09 at 21:56
  • @SergioTapia _"call"_ is dev talk for "run" or "execute" (because you _call_ methods or functions in a code) – Bernardo Dal Corno Mar 25 '18 at 07:08
  • not default, but on linux, to run by do "filename.py", you have to add following line to first line : #!/path/to/python – sailfish009 Sep 19 '19 at 04:24
15

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.
Matt
  • 74,352
  • 26
  • 153
  • 180
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
4

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.

Transients
  • 41
  • 1
1

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

keneth
  • 197
  • 3
  • 4
1

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.

1

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.

Karen
  • 21
  • 1
1

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

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
1

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

shiv
  • 11
  • 4
  • Welcome to Stack Overflow! Please note you are answering a very old and already answered question. Here is a guide on [How to Answer](http://stackoverflow.com/help/how-to-answer). It seems to me this is not an answer to the question of the OP. – help-info.de Sep 14 '18 at 17:18
0

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.

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26