Tyler
Tyler

Reputation: 4007

windows command line and Python

I have a python script that i want to run from the command line but unsure how to run it. Thanks :)

Upvotes: 1

Views: 8838

Answers (6)

gimel
gimel

Reputation: 86344

See Basic Hints for Windows Command Line Programming.

If your python installation directory is included in %PATH% -

C:\> python myscript.py

If you know the installation path:

C:\> C:\python26\python myscript.py

And, you can insert a hashbang in the 1st line of the script:

#! C:\python26\python

and it will run by typing just the script name. This is the content of p.py:

#!C:\python26\python
import sys
print sys.path

And calling it directly from a cmd.exe window:

C:\>p.py
['C:\\WINDOWS\\system32\\python26.zip', 'C:\\Python26\\DLLs',
'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win',
'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 
'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib]

Upvotes: 3

Andy
Andy

Reputation: 2830

You might find it useful to include a .bat file which calls the .py script. Then all you need to do is to type the name of your script to run it.

Try something like: python %~dp0\%~n0.py %*

(From http://wiki.tcl.tk/2455)

Upvotes: 1

Anurag Uniyal
Anurag Uniyal

Reputation: 88727

  1. do you have python installed? if not install it from python.org
  2. on command line use

    python "path to script.py"

  3. if python is not in PATH list you can add it to PATH in environment variables or directly use path to python.exe e.g.

        c:\python25\python.exe myscript.py
    

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319521

I do it this way:

C:\path\to\folder> yourscript.py

Upvotes: 4

David Cournapeau
David Cournapeau

Reputation: 80712

If your script is foo.py, you can simply do

C:\Python25\python.exe foo.py

Assuming you have python 2.5 installed in the default location. Alternatively, you can add C:\Python25 to your %PATH%, so that:

python foo.py

will work. But be aware that changing %PATH% may affect applications (that's why it is not done by the python installer by default).

Upvotes: 2

Joey
Joey

Reputation: 354356

python myscript.py

Upvotes: 3

Related Questions