Writing Portable Shebangs
00:00 The last lesson you saw the shebang in action in a standalone Python program. Now, in this lesson, you learn about defining portable shebangs. As discussed earlier, specifying the interpreter path with a shebang is done with absolute paths and not relative paths.
00:15
But having a fixed absolute path in a shebang means that your script may not work on everyone’s system because there might be some slight differences in the path specified to find the Python interpreter. To fix this, developers adopt a workaround method with the /usr/bin/env
command, which you and I will look at now, The /usr/bin/env
command is used to see all set environment variables defined in your shell, but for our context, you will use it to figure out the actual path to the Python interpreter in the environment where the script is being run.
00:55
This is usually in a variable called PATH
in the shell’s environment.
01:00
Now, in your terminal, if you call this command /usr
/bin/env
without any argument, you should see all the set environment variables defined in your shell.
01:12
The one of interest for finding the Python interpreter will be this one called PATH
.
01:20
But with /usr/bin/env
, you can run programs in modified environments if you’d want to by altering certain variables where needed. So if you call /usr/bin/env
space python3
, it’ll find the first occurrence of the specified executable, in this case, python3
in the PATH
variable, and then it’ll run it.
01:44 So if I do this, I’m taken into an interactive Python shell, also called the REPL.
01:49
I’ll exit this now using the exit()
01:53
because I don’t want to do much here. But using the /usr/bin/env
is how you define flexible shebangs, because this way, no hardcoded Python interpreter is used.
02:03 Instead, it looks for the interpreter in the user’s environment when executing. This is actually one of the principles upon which Python virtual environments are built.
02:13
A Python virtual environment is an isolated space where you can install Python packages separately from the global Python installation. So if I create a new virtual environment with python3
-m venv
module and create a virtual environment called venv
, by running this, I should now have a directory called venv
.
02:36
And to activate this virtual environment using source venv
/bin/activate
, I now have this virtual environment activated as specified with venv
shown in parentheses.
02:50
With my virtual environment activated, if I say which python3
, I’m pointed to my Python interpreter in the virtual environment I just created.
03:01
So if I were to execute a script with this virtual environment activated, this is the interpreter that will be used. But why is this? It’s happening because if I check my PATH
variable again, to view a variable, you can use the echo
command and I’ll call the variable PATH
.
03:17
I can see my venv
binary put at the top of the list.
03:22
And like I said earlier, it finds the first occurrence of the specified executable of Python in the PATH
variable and uses it. That’s why with a virtual environment activated, it’s the python3
interpreter for that virtual environment that is used for script execution.
03:40
Now, if I modify my shebang_example.py
, in the shebang from the hardcoded absolute path to /usr/bin/env
python3
.
03:54
You now know how to define portable shebangs that can work in anyone’s environment, by not coding your own Python interpreter, but instead using /usr/bin/env
.
04:06 In the next lesson, you learn some best practices when using the shebang.
Become a Member to join the conversation.
7ssanM on June 7, 2025
This method does not work well when using an anaconda3 on Mac os , the %PATH environment variable does not contain the word python nor python3 as the python interpretter is placed in “opt/anaconda3/bin” , is there any solution to this as no guarantee which environment management is the used… thanks alot