How to execute Python scripts in Windows?


I have a simple script (using Python 2):

import sys
print sys.argv[1]

If I execute my script by:

python c:/..../ argument

It prints argument but if I execute script by: argument

error occurs:


So arguments do not pass to script.

python.exe in PATH. Folder with also in PATH.
python.exe is default program to execute *.py files.

What is the problem?

6/8/2016 7:00:30 AM

Accepted Answer

When you execute a script without typing "python" in front, you need to know two things about how Windows invokes the program. First is to find out what kind of file Windows thinks it is:

    C:\>assoc .py

Next, you need to know how Windows is executing things with that extension. It's associated with the file type "Python.File", so this command shows what it will be doing:

    C:\>ftype Python.File
    Python.File="c:\python26\python.exe" "%1" %*

So on my machine, when I type " foo", it will execute this exact command, with no difference in results than if I had typed the full thing myself:

    "c:\python26\python.exe" "" foo

If you type the same thing, including the quotation marks, then you'll get results identical to when you just type " foo". Now you're in a position to figure out the rest of your problem for yourself.

(Or post more helpful information in your question, like actual cut-and-paste copies of what you see in the console. Note that people who do that type of thing get their questions voted up, and they get reputation points, and more people are likely to help them with good answers.)

Brought In From Comments:

Even if assoc and ftype display the correct information, it may happen that the arguments are stripped off. What may help in that case is directly fixing the relevant registry keys for Python. Set the


key to:

"C:\Python26\python26.exe" "%1" %*

Likely, previously, %* was missing. Similarly, set


to the same value. See

example registry setting for python.exe HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command The registry path may vary, use python26.exe or python.exe or whichever is already in the registry.

enter image description here HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

4/18/2017 11:03:49 PM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow