Loading video player…

Making Your File Executable With a Shebang

00:00 In Python, generally if we want to execute a Python script, we type the python command, and then the name of a text file which ends in .py and contains our Python code.

00:13 So in this example, it runs our quote generator, which gives us an inspirational quote of the day. But with the addition of this thing called a shebang, we can also run it without typing the python command, as if the file was an executable application on its own.

00:30 Now, a shebang is called a shebang because of the two characters that it starts with. So the first one is the hash character, which is used for comments in Python, so the shebang is actually ignored when you run the contents of this file as a Python file because Python treats it as a comment.

00:50 But this character in music is also used to indicate sharp notes, so that’s the sh part of the word shebang, and then the exclamation mark is often referred to as a bang character, presumably because loud explosions in text would also end with an exclamation mark.

01:07 So those two characters together are called a shebang. Now, a shebang is a single line that we add to the top of a script file to indicate to the system how this file should be run, which executable or which tool should run the contents of this file.

01:24 So this typically points to this folder, the /usr/bin/env folder, because that is the typical installation path for Python in a Unix-based system, but that could be any valid path to some kind of executable which runs the contents of your script.

01:41 Let’s take a look at this in action. Here is our quote_generator.py file. We will talk about the contents of it in detail over the next few lessons, but for now you can see that at the very top of this file, it starts with the Python code.

01:58 So this means we can run it in the way that we saw in the terminal with the python command, but if we want to make it runnable on its own without having to type in the python command every time, we need to make sure the file starts with this shebang.

02:12 So to do that, I’m going to type these two characters to make sure they’re the sh and the bang part of shebang, and then we will type this typical portable version of the shebang.

02:26 So the final shebang at the top of the file reads hash, exclamation mark, forward slash, usr, forward slash, bin, forward slash, env — that’s /usr/bin/env, folders that contain the Python binary, and then space, python3.

02:43 We’ll save that file. Now remember, a shebang has to be the very first line in a file for it to work correctly. Now we’ll go back over to the terminal.

02:54 Now in here, because we’ve added that shebang, I can type ./ and just the name of the file,

03:01 and because of the shebang, the system will know that the contents of this file should be sent to Python and executed. So we get the same kind of output as if we’d run it with the python command.

03:13 Now, the things to remember about the shebang are that they really are just an optional shortcut at the top of the file to make the file executable directly.

03:23 It doesn’t alter the contents of the Python code in any way, because Python really just interprets that as a comment line. Shebangs only work on Unix-based systems, so Linux, macOS, or even Unix-based terminals on Windows such as Git Bash.

03:39 For a shebang to work, it needs to be the first line in the file, and it needs to point to a valid Python binary. So now we’ve seen how we can execute our Python script in the terminal.

03:52 The next few lessons will look at how to structure this Python script in the first place with coding best practices.

Avatar image for Cosimo Trinca

Cosimo Trinca on June 3, 2026

Great video, David.

I think a minor correction might be needed in the explanation of /usr/bin/env: the video states that this is the common installation directory for python; in reality it is actually the path to the Unix command env, with python3 being the argument (hence the space in between).

/usr/bin/env python3 executes the command python3 in the current environment. Alternatively, the full path to the interpreter you want to use can be given (e.g. #!/usr/bin/python3).

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on June 5, 2026

@Cosimo Trinca Good catch, and thanks for the clear write-up. You’re right on both counts.

/usr/bin/env isn’t a directory where Python lives. It’s the path to the env program itself (the executable sits in /usr/bin/). When the system reads the shebang #!/usr/bin/env python3, it runs env with python3 as an argument, and env then looks up python3 on your PATH and hands the script over to it. That indirection lets your shell pick up whichever python3 is first on your PATH (for example, the one inside an activated virtual environment) instead of committing to a fixed location.

As you noted, the alternative is to hardcode the interpreter directly, like #!/usr/bin/python3, which skips env but ties the script to one specific interpreter path.

Appreciate you taking the time to spell it out 🙂

Become a Member to join the conversation.