Taylor Liss
Taylor Liss

Reputation: 593

What is the difference between these 4 different types of Python shebangs on Windows?

I just came across shebangs (#!) for the first time while learning Python and I'm still trying to understand how exactly they work. After doing some research, I landed on this page where it listed four different types of shebangs. However, I'm not really sure what the difference is in Windows.

#! /usr/bin/env python
#! /usr/bin/python
#! /usr/local/bin/python
#! python

Now, I'm betting that the first one has something to do with virtual environments, but I'm not quite sure what. And according to this StackOverflow thread, the paths listed above actually are for POSIX systems, not for Windows... which confounds me even more because they are somehow being translated into Windows directories through some magic. Here's a quote:

A bin directory is created on POSIX systems only . . . Some paths within the virtualenv are slightly different on Windows: scripts and executables on Windows go in ENV\Scripts\ instead of ENV/bin/ and libraries go in ENV\Lib\ rather than ENV/lib/.

Can anyone give a beginner a little more information on how exactly shebangs like these work on Windows?

Upvotes: 2

Views: 235

Answers (1)

BrenBarn
BrenBarn

Reputation: 251373

The documentation is not totally explicit, but on my reading of it, there is no difference between those shebang lines. The shebang handling on Windows is purely "virtual" --- that is, the paths in the shebang lines are not actually mapped onto any paths on the Windows file system. Rather, the use of any of these "virtual paths" simply means "use the default system Python when running this file via the py Python launcher". The purpose of allowing the shebang line on Windows is to let the Python script specify arguments to Python, or Python versions (e.g., python3). You can find more info about how the default system Python is determined, how to override it, etc., in the documentation linked above.

Incidentally, note that on Windows these shebangs are only used when you run the Python script using the py launcher.

Upvotes: 3

Related Questions