tags:

views:

291

answers:

6

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?

A: 

Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py

Geo
+2  A: 

If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.

Thomas Owens
+9  A: 

Wouldn't you simply save your Python code into a file, and then execute that file using Python?

Save your code into a file called Test.py.

And then run it?

$ C:\Python24\Python.exe C:\Temp\Test.py
quip
A: 

In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]

Nick T
+5  A: 

If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.

If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.

When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.

If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.

RedGlyph
+1 for idle :) it comes in handy all the time
Jiaaro
It does! :-) And it's often overlooked, even though it's already installed.
RedGlyph
+1  A: 

A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.

EOL