I’m not on Windows, but I believe that just having the .py extension and
#!/usr/bin/env python3 shebang line will made a script executable,
at least from the command line. AIUI the
.py file association invokes
the Python launcher, which understants the shebang line.
I don’t think you need to make an “exe” even on Windows.
On UNIX/POSIX (eg MacOS or Linux) scripts can be made genuinely
executable by (a) setting the read and execute permissions and (b)
setting the correct shebang line, which is usually
When you try to execute a command on a UNIX/POSIX system either at the
command line or via the OS
exec*p() call, the following happen:
- if the command is just a word instead of the full path to an
executable, the full path is inferred by looking for executable files
$PATH environment variable
- having found the executable, the loader looks at the file header
- a binary file is loaded into memory and executed, but a script with a
shebang line is run via an interpreter
That last bit makes scripts directly executable, again without making
The usual shebang line for a Python script is:
The format is:
#! which is the “shebang”, indicating that this is a script to be
run by an interpreter such as Python
- the next bit is the path to the interpreter
- optionally followed by an argument for the interpreter
I’m going to show the direct version first:
If that’s in your script and you ran the command:
your-script-name some arguments
this would cause the OS to invoke the executable
the argument list:
/usr/bin/python3 the-path-to-your-script some arguments
/usr/bin/python3 is a real binary executable, and the above invocation
python3 to open your script and run it with
This is why scripts need both the execute and read permissions: the
execute permission is used by the OS to find the script as an
“executable” and the read permission is needed by the interpreter
because it literally has to open the script and read its contents.
So the shebang line is a little indirection understood by the OS
itself which invokes some interpreter on your script file. By invoking
/usr/bin/python3 it causes the script to be interpreted as Python.
By contrast, shell scripts start:
which will invoke the executable
/bin/sh (available on all
UNIX/POSIX systems, providing a Bourne shell) on a script. That makes a
shell script a shell script.
Returning to the original shebang:
this is a bit of a gimmick. Python3 is not always installed in the same
place, or you may want to run your “preferred” python3. The
command will find
python3 in your
$PATH, thus choosing the
which would be found if you invoked
The short summary: you do not normally need to “make an exe”.
On UNIX/POSIX the OS directly understands the shebang line.
On Windows the python launcher is associated with the
and the luancher understands the shebang line.
Cameron Simpson firstname.lastname@example.org