Better Python Integration in Windows: Shebangs and Version Selectors

I’ve written several times on efforts to make Python better on Windows. I also have an outstanding request to the Windows 10 team to get Python built directly into Windows 10 (please upvote it!). In this post, I’ll show you some very simple techniques to put Python on Windows relatively on par with Python on OS X and Linux.

On OS X and Linux, we can use a special kind of comment at the top of our script called a shebang.

#!/usr/bin/env python3
# normal python code here...

This tells the OS which Python runtime to select and allows the script to be executed directly. For example, imagine that file was named program.py. Without the shebang, if we type simply ./program.py we likely will get an error. We have to specify which version of Python by running python3 ./program.py. This has several problems. It’s not obvious to users they have to do this to run the script. Moreover, if that program is for Python 2 and we guess to use Python 3 too bad for us! The shebang solves both of these for us… on OS X and Linux.

Shebangs on Windows

If you try this little trick on Windows, you are likely to be disappointed even if you have Python installed. One possibility is to get this lovely experience.

Another possibility is you’ll see a dialog to choose how to run *.py files.

If you pick Python.exe you may get the right outcome. But now all Python scripts are  tied to a single Python version (Python 2 or Python 3). What we need is to fix this with something like the shebangs of Linux.

Consider this simple program that states the version of Python executing it.

import sys

ver = sys.version_info[0]
print("Hello from Python {}".format(ver))

Notice that if we directly execute it by specifying the Python version to run, it works great.

Nice. But we still have the problem of the user deciding which version of Python to try (not good). Since Python 3.3, they have introduced a program named Python Launcher for Windows. This lovely little program allows us to use shebangs to let the script select its version.

#!/usr/bin/env python2
import sys

ver = sys.version_info[0]
print("Hello from Python {}".format(ver))

Now if we have the Python 2 shebang or Python 3 shebang, we get the perfect behavior. This is as long as we directly specify the Python launcher (c:\windows\py.exe)

Note we can even keep the /usr/bin/env for compatibility with Linux & OS X. But directly executing the script still fails.

Luckily, we can teach Windows to use the Python Launcher rather than locking to a particular installed version of Python. In an admin command prompt, run these two commands.

Success

Now, try to directly execute your script. Notice that simply changing the shebang controls the version selected by the operating system.

Props to my DevelopMentor colleague Pinku Surana for the tips about putting all of these together.

Learning Python?

If you or your team need to get up to speed on Python, consider my series of Python classes at DevelopMentor.

https://www.develop.com/training-courses/python

4 comments

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s