1

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?

3
  • 3
    Do you really mean running it under a DOS operating system, or just in the Windows command line interpreter? Commented Oct 23, 2009 at 17:37
  • I was gonna say. I'd be very surprised if there's a version of Python that will run under MS-DOS. Commented Oct 24, 2009 at 22:02
  • @Robert Rossney: caddit.net/pythond Commented Dec 14, 2009 at 19:30

7 Answers 7

10

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
Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

It does! :-) And it's often overlooked, even though it's already installed.
3

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.

Comments

1

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

Comments

1

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.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.