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.

Become a Member to join the conversation.