dave.j.lott
dave.j.lott

Reputation: 1019

Running Python from the Windows Command Line

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?

Upvotes: 1

Views: 15731

Answers (7)

BenjaminRH
BenjaminRH

Reputation: 12172

You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables

Upvotes: 1

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94595

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.

Upvotes: 1

quip
quip

Reputation: 3694

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

Upvotes: 10

RedGlyph
RedGlyph

Reputation: 11579

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.

Upvotes: 5

Nick T
Nick T

Reputation: 26747

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

Upvotes: 0

Thomas Owens
Thomas Owens

Reputation: 116187

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.

Upvotes: 3

Geo
Geo

Reputation: 96947

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

Upvotes: 1

Related Questions