I have a simple script blah.py (using Python 2):
import sys
print sys.argv[1]
If I execute my script by:
python c:/..../blah.py argument
It prints argument but if I execute script by:
blah.py argument
error occurs:
IndexError...
So arguments do not pass to script.
python.exe in PATH. Folder with blah.py also in PATH.
python.exe is default program to execute *.py files.
What is the problem?
Simply run the command:
Assuming the file name is within same folder and Python has already been added to environment variables.
you should make the default application to handle python files be python.exe.
right click a *.py file, select "Open With" dialog. In there select "python.exe" and check "always use this program for this file type" (something like that).
then your python files will always be run using python.exe
Additionally, if you want to be able to run your python scripts without typing the
.py
(or.pyw
) on the end of the file name, you need to add.PY
(or.PY;.PYW
) to the list of extensions in the PATHEXT environment variable.In Windows 7:
right-click on Computer
left-click Properties
left-click Advanced system settings
left-click the Advanced tab
left-click Environment Variables...
under "system variables" scroll down until you see PATHEXT
left-click on PATHEXT to highlight it
left-click Edit...
Edit "Variable value" so that it contains
;.PY
(the End key will skip to the end)left-click OK
left-click OK
left-click OK
Note #1: command-prompt windows won't see the change w/o being closed and reopened.
Note #2: the difference between the
.py
and.pyw
extensions is that the former opens a command prompt when run, and the latter doesn't.On my computer, I added
;.PY;.PYW
as the last (lowest-priority) extensions, so the "before" and "after" values of PATHEXT were:before:
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
after
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
Here are some instructive commands:
On Windows,
To run a python module without typing "python",
--> Right click any python(*.py) file
--> Set the open with property to "python.exe"
--> Check the "always use this program for this file type"
--> Append the path of python.exe to variable environment e.g. append C:\Python27 to PATH environment variable.
To Run a python module without typing ".py" extension
--> Edit PATHEXT system variable and append ".PY" extension to the list.
I encountered the same problem but in the context of needing to package my code for Windows users (coming from Linux). My package contains a number of scripts with command line options.
I need these scripts to get installed in the appropriate location on Windows users' machines so that they can invoke them from the command line. As the package is supposedly user-friendly, asking my users to change their registry to run these scripts would be impossible.
I came across a solution that the folks at Continuum use for Python scripts that come with their Anaconda package -- check out your Anaconda/Scripts directory for examples.
For a Python script
test
, create two files: atest.bat
and atest-script.py
.test.bat
looks as follows (the.bat
files inAnaconda\Scripts
callpython.exe
with a relative path which I adapted for my purposes):test-script.py
is your actual Python script:If you leave these two files in your local directory you can invoke your Python script through the
.bat
file by doingIf you copy both files to a location that is on your
PATH
(such asAnaconda\Scripts
) then you can even invoke your script by leaving out the.bat
suffixDisclaimer: I have no idea what's going on and how this works and so would appreciate any explanation.
Found an incredibly useful answer here: How to run different python versions in cmd?
As J.F. Sebastian suggests, Python Launcher for Windows is the best and default choice for launching different version of Python in Windows. It used to be a third-party tool, but now it is officially supported since Python 3.3.
This is a great tool just use it!