<

Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x

Do you know the "Python Launcher for Windows"?

Published on
17,706 Points
7,806 Views
4 Endorsements
Last Modified:
Awarded

Introduction

On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced with the version. This article was written for those of you who work with more than one version of Python on one computer, and who have not noticed the Python Launcher yet.
 

Summary

If you set the environment correctly, the Python launcher allows you to launch both your Python 2 and Python 3 scripts the same way:

py older2.py
py newer3.py

Open in new window

Working from cmd window, you can also left out the py and launch it as if the script were executable:

older2.py
newer3.py

Open in new window

You can even omit the extension when launching from the cmd console:

older2
newer3

Open in new window

or--if you prefer--it allows you to launch the Python application using mouse double-click on the script icon. The version of Python that is to be used for interpreting the script can now be captured inside the script. And this is a good thing.
 

Unlucky scenario in Windows (compare to Unix-like systems)

First, a bit of Unix evangelism for those who went astray...

When MS Windows is finished, it will be the best documented Unix in the World.
                              (A prophet unknown to me -- some say that Joker is her name.)
Unix uses very clear, simple, and clever abstractions from the very beginning. A file is a stream of bytes. There is a structure bound to the stream of bytes that represents the stream of bytes as a pure file abstraction -- think about the anonymous structure (i.e. unnamed, identified only by a number). Having access to that structure, you have access to the file bytes. The structure captures also attributes of the file. One of the attributes is the flag that says "this file is executable". A directory is just another file (with the directory flag) that bounds the file id's (numbers) with the file names. Nothing more, nothing less.

Again, the executable file is marked as executable solely by the above mentioned file attribute. No obligatory extensions are forced, no special names are needed. When you type its name on the command line, the Unix-like operating system launches the file. How does the OS knows the way the file should be launched? How does it distinguish a pure binary executable from a shell script, or from a Python script? The answer is hidden in the first two bytes.

The first two bytes in the executable files are used for decision how the file is to be launched. They are also known as a magic number. To make the story short, the #! (sometimes called hash-bang) says it is a text file that should be interpreted. The OS launcher then gets the following string and thinks about the string as about the path to the interpreter that is capable to interpret this script. The launcher starts the interpreter and passes the file name and the other command-line arguments to it. And you know the rest... (hint: Syntax error... -- just kidding. No, no, no. Yes, yes! :) Now you know ("You..., and you..., poor Windows apple-pie eaters...") why the Linux true men write the funny #!/usr/bin/env python3 at the beginning of the script.

What about the Windows? The OS designers still use a magic number (occasionally). When you know how to look inside any executable (*.exe or *.com), you will find MZ in the first two bytes. On the other hand, you will find nothing special in a *.bat. "And I could bet you--poor Windows apple-pie eaters--write nothing like that at all in your Python scripts for Windows! Shame, shame, shame..."

Say where they are, and cause that I may know them;
For great desire constraineth me to learn
If Heaven doth sweeten them, or Hell envenom.
 
And he: They are among the blacker souls;
A different sin downweighs them to the bottom;
If thou so far descendest, thou canst see them.
                        (Dante Alighieri, Divine Comedy)

"Let there be light"

Let there be light to illuminate the path to your better Windows Python-programmer future. You use more Python versions in Windows. You know they are installed at:
 
c:\Python23
c:\Python25
c:\Python26
c:\Python27
c:\Python32

Open in new window

Say, c:\Python27 and c:\Python32 is in your PATH. Some of your scripts need Python 2.x, some newer scripts need Python 3.x. For working with command line, you have renamed c:\Python32\python.exe to c:\Python32\python3.exe. Then you can launch older scripts as

python older.py

Open in new window

and newer scripts as

python3 newer.py

Open in new window

And because you start to use more and more of the newer scripts (for Python 3), you have later renamed c:\Python32\python3.exe to c:\Python32\py.exe. And the life is easier now.

py newer.py

Open in new window


"Hey! Python 3.3 was just released!" And now you have also

c:\Python33\python.exe

Open in new window

immediately renamed to the c:\Python33\py.exe. You have also changed the PATH to prefer the newer Python 3.3...

c:\myproject>py newer.py
  File "newer.py", line 3
SyntaxError: Non-ASCII character '\xc5' in file newer.py on line 4, 
but no encoding declared; see http://www.python.org/peps/pep-0263.html 
for details

Open in new window

"What is that?"

c:\Python33\py.exe newer.py
Succeeded.

Open in new window

!@#$%^&* Windows *&^%$#@! (censored)

After some unlucky attempts to find what happens (and because Windows does not have the which utility) you start to search for the py.exe on your disk (Alt-F7 in your Total Commander, being too lazy to look only in the PATH directories -- which is the task for the missing which utility...). And you found:

C:\Windows\py.exe

Open in new window

... and the date of the file is new. It is the time to read the doc: What’s New In Python 3.3...

PEP 397: Python Launcher for Windows
The Python 3.3 Windows installer now includes a py launcher application that can be used to launch Python applications in a version independent fashion.
...
3.4. Python Launcher for Windows
New in version 3.3.
The Python launcher for Windows is a utility which aids in the location and execution of different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version.
...
PEP 397 -- Python launcher for Windows
Sigh...

OK. What should I do. But, you know, I am in a hurry...


Download the Python 3.3 (or newer, if you read it later), and install the new Python.

Remove the py.exe from your c:\Python* directories (if you have any py.exe there).

Type py to the command line. It should launch Python 2.x (the highest version that is installed) in the interactive mode. Check the version (see the first output line in the console). Quit the interactive mode of the Python 2.x. (Yes, it launches Python 2.x. Think about py.exe being Python version independent, and the 2.x is preferred. But you can explicitly say what version and even what variant--like 32/64-bit--should be launched via the first option.)

Type py -3 (py space dash three) to the command line. It should launch Python 3.x (the highest version that is installed) in the interactive mode. Check the version (see the first output line in the console). Quit the interactive mode of the Python 3.x.

Open one of your Python 2 script files (say my2old.py) in your favourite editor, and put the #!python2 to its first line. Type py my2old.py to the command line (i.e. py space filename), and check whether the script works correctly.

Open one of your Python 3 script files (say my_new3.py) in your favourite editor, and put the #!python3 to its first line. Type py my_new3.py to the command line (i.e. py space filename), and check whether the script works correctly.

Open the file explorer (or Total Commander, or whatever similar application), double click on the my2old.py and check whether it works correctly.

Similarly, double click on the my_new3.py and check whether it works correctly.

If you have any Python applications with GUI (i.e. the one that creates a non-console window--written in Tkinter, wxPython, PyQT--, change its extension to .pyw, put the above mentioned #!python2 or #!python3 to its first line, and try.

Read the documentation if you want to learn more about the Python Launcher for Window.

Add a comment to this article if you have anything to tell or to ask.
Have a nice day (independently on the weather). And do not spend so much time with the computer keyboard. It is not that much healthy... you now... should move your body, train the muscles, not only your brain :)
4
Comment
Author:pepr
1 Comment
 
LVL 15

Expert Comment

by:Eric AKA Netminder
Congratulations; your article has been published.

ericpete
Page Editor
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Suggested Articles

Title Views Activity
To Py or not to Py 3,374
Variables in Python 2,591
Modules in Python 1,956
Saving programs in Python 3,283
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month