How to execute Python scripts on Windows?

I have a simple blah.py script (using Python 2):

import sys print sys.argv[1] 

If I ran my script with:

 python c:/..../blah.py argument 

It prints the argument, but if I execute the script by:

 blah.py argument 

an error occurs:

IndexError ...

So, the arguments do not go to the script.

python.exe in PATH. The folder with blah.py is also in PATH.
python.exe is the default program for executing * .py files.

What is the problem?

+83
python command-line windows scripting file-association
Dec 20 '09 at 2:09
source share
8 answers

When you execute a script without typing "python" in front, you need to know two things about how Windows calls the program. First you need to find out which Windows file considers:

     C: \> assoc .py
     .py = Python.File

Next, you need to know how Windows performs actions with this extension. It is associated with the file type "Python.File", so this command shows what it will do:

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

So, on my machine, when I type โ€œblah.py fooโ€, it will execute this exact command without any difference in results than if I typed the whole thing myself:

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

If you type the same thing, including quotation marks, then you will get results identical to when you simply type "blah.py foo". Now you are able to figure out the rest of your problem for yourself.

(Or post more useful information in your question, for example, actual cut and paste copies of what you see on the console. Note that people who do such things vote for their questions and get reputation points, and more people probably help them with good answers.)

Introduced from comments:

Even if the assoc and ftype attributes display the correct information, it may happen that the arguments are disabled. What might help in this case is a direct fix to the appropriate registry keys for Python. Set

 HKEY_CLASSES_ROOT\Applications\python26.exe\shell\open\command 

for:

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

Probably, earlier, %* absent. Similarly, set

  HKEY_CLASSES_ROOT\py_auto_file\shell\open\command 

to the same value. See http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

example registry for python.exe HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command The registry path may differ, use python26.exe or python.exe or depending on what is already in the registry.

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

+130
Dec 20 '09 at 15:05
source share

you must make the default application for processing python be python.exe files.

right-click the * .py file, select "Open With." There, select "python.exe" and check "always use this program for this type of file" (something like this).

then your python files will always start using python.exe

+22
Dec 20 '09 at 2:32
source share

Also, if you want to be able to run python scripts without typing .py (or .pyw ) at 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 on the Advanced tab
left click environment variables ...
under "system variables" scroll down until you see PATHEXT
left click on PATHEXT to select it
left click Modify ...
Change the "variable value" so that it contains ;.PY (the final key will skip to the end)
left click OK
left click OK
left click OK

Note # 1: command line windows do not see that the change will not be closed and reopened.

Note # 2: the difference between the .py and .pyw is that the first command launches the command line at startup, and the second does not.

On my computer, I added ;.PY;.PYW as the last (with the lowest priority) extensions, so the values โ€‹โ€‹for the "before" and "after" PATHEXT were:

to: .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 instructional commands:

 C:\>echo %pathext% .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW C:\>assoc .py .py=Python.File C:\>ftype Python.File Python.File="C:\Python32\python.exe" "%1" %* C:\>assoc .pyw .pyw=Python.NoConFile C:\>ftype Python.NoConFile Python.NoConFile="C:\Python32\pythonw.exe" "%1" %* C:\>type c:\windows\helloworld.py print("Hello, world!") # always use a comma for direct address C:\>helloworld Hello, world! C:\> 
+14
Oct 23 '12 at 5:15
source share

How to execute Python scripts on Windows?

You can install pylauncher . It is used to run .py, .pyw, .pyc, .pyo files and supports several Python installations:

 T\:> blah.py argument 

You can run your Python script without specifying the .py extension if the PATHEXT environment variable has .py, .pyw:

 T:\> blah argument 

It adds support for the shebang title bar ( #! ) To select the correct version of Python for Windows if you have multiple versions installed. You can use the * nix compatible syntax #! /usr/bin/env python #! /usr/bin/env python .

You can specify the version explicitly, for example, to run using the latest installed version of Python 3:

 T:\> py -3 blah.py argument 

It should also fix your sys.argv problem as a side effect.

+3
Oct 23
source share

I ran into the same problem, but in the context of the need for a package of my code for Windows users (from Linux). My package contains several scripts with command line options.

I need these scripts to install in an appropriate location on Windows users' computers so that they can call them from the command line. Since 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 people at Continuum use for the Python scripts that come with their Anaconda package. For an example, browse the Anaconda / Scripts directory.

For a Python script test create two files: a test.bat and a test-script.py .

test.bat looks like this ( .bat files in Anaconda\Scripts call python.exe with a relative path that I adapted for my purposes):

 @echo off set PYFILE=%~f0 set PYFILE=%PYFILE:~0,-4%-script.py "python.exe" "%PYFILE%" %* 

test-script.py is your actual Python script:

 import sys print sys.argv 

If you leave these two files in your local directory, you can call your Python script through the .bat file by doing

 test.bat hello world ['C:\\...\\test-scripy.py', 'hello', 'world'] 

If you copy both files to a location that is located on your PATH (for example, Anaconda\Scripts ), you can even call your script, leaving the suffix .bat

 test hello world ['C:\\...Anaconda\\Scripts\\test-scripy.py', 'hello', 'world'] 

Disclaimer: I have no idea what is happening and how it works, and therefore would be grateful for any explanation.

+3
Nov 20 '13 at 22:03
source share

On windows

In run python module without typing "python" ,

-> Right click on any python file (*. Py)

-> Set the open property to "python.exe"

-> Check "always use this program for this file type"

-> Add the path to python.exe to the environment variable, for example. append C: \ Python27 to the PATH environment variable.

In run the python module without typing the extension ".py"

-> Change the system variable PATHEXT and add the extension ".PY" to the list.

+2
Jun 23 '15 at 3:26
source share

Can you execute python.exe from any card? If you do not, try if you have the correct values โ€‹โ€‹for python.exe in the PATH environment

You are in the same directory as blah.py. Check this by writing the command -> edit blah.py and check if you can open this file

EDIT:

In this case you cannot. (python arg means that you are calling python.exe with some parameters, which python assumes is the name of the script file you want to run)

You can create bat file whit lines on the path map and run the .bat file

Example:
On one of the Path maps, create blah.py.bat. Edit the file and place the line

 python C:\Somedir\blah.py 

Now you can run blah.py from any of them, because you do not need to put the .bat extension when you run bat files.

0
Dec 20 '09 at 2:21
source share

An incredibly useful answer is found here : How to run different versions of python in cmd?

I would suggest using the Python Launcher utility for Windows, which was introduced in Python 3.3 some time ago. You can also manually download and install it directly from the authorโ€™s website for use with earlier versions of Python 2 and 3.

No matter how you get it, after installation it will be associated with all the standard Python file extensions (i.e...py, .pyw, .pyc and .pyo files). You will not only be able to explicitly control which version is used on the command line, but also based on script -by-script by adding Linux / Unix-y shebang #! / Usr / bin / env pythonX at the beginning of your Python scripts.

As J.F. Sebastian says, Python Launcher for Windows is the best and default choice to run another version of Python on Windows. It was a third-party tool, but now it is officially supported with Python 3.3.

New in version 3.3.

The Python Launcher for Windows is a utility that helps you host and run different versions of Python. This allows scripts (or the command line) to indicate preference for a particular version of Python and will find and execute that version.

This is a great tool, just use it!

0
Mar 16 '14 at 16:13
source share



All Articles