Loading video player…

Introducing Python Shebang

Resources linked in this lesson:

00:00 The shebang is a small but incredibly powerful element used when running executable scripts. The term shebang refers to the hash symbol and exclamation mark, hence the name shebang.

00:12 It’s also sometimes called a hashbang, and wherever you find a shebang, it’s usually followed by the path to an interpreter such as python, bash, or perl.

00:23 The shebang plays a crucial role in script execution in Unix based operating systems like Linux and macOS. When a Unix operating system shell tries to execute a script, if it starts with a hash symbol and an exclamation mark, the rest of that line is taken to be the path to an interpreter program that should be used to execute that script.

00:44 Now, why is this shebang line important? Without a shebang, if you try to execute a Python script directly from the command line, for certain cases where you might need to make a script executable on its own.

00:56 For example, you had a script called ./script.py, and want to call this without using the python or python3 command.

01:03 If you do this and don’t have a shebang, the operating system shell might not know how to handle it, which can lead to errors or unexpected behavior. I’ll give you a cooking analogy, if you are into cooking. Imagine you have a recipe written in a specific language, say Spanish. If someone who only speaks English tries to read it directly, they won’t understand the instructions, would they?

01:26 But if you specify within the recipe a note saying, this recipe needs to be translated by a Spanish speaker, the recipient can then hand it to the right person for proper execution.

01:37 The shebang acts like that little note for your Python scripts. It tells your operating system shell, Hey, to run this Python file, you need to use the Python interpreter located at a specific path.

01:49 Use it to run the rest of this file.

01:52 The typical syntax for a Python shebang looks something like this: #!/usr/bin/python3 or #!/usr/bin/env python3 is usually the absolute path to the Python interpreter in a Unix-based operating system.

02:11 So whenever you execute a Python script from the terminal, if it has a shebang, the system knows exactly which interpreter to use.

02:19 A quick note: When using the shebang, for it to work as intended, it must be placed at the very first line of your script at the top with no spaces and blank lines above it.

02:30 Now, when might you need the shebang? If you’re working in a Unix-like operating system like Linux and macOS, you might need the shebang if you want your Python script to be directly executable, or you want to ensure the correct interpreter is used automatically on script execution.

02:48 The shebang is a Unix-based convention—that is, it’s only applicable in Unix-based operating systems like Linux and macOS. In a Windows terminal environment, it means nothing and is seen as just an ordinary comment.

03:02 So if you’re on Windows and you want the same effect in a Windows environment that is, specifying the interpreter to be used, you need to install Windows Subsystem for Linux (WSL). This comes with a Unix shell, and there the shebang can be used.

03:18 Alternatively, Windows lets you make what is called a global file association between a file extension like .py Python files, and programs like the Python interpreter.

03:29 This is how you can achieve a similar effect to the shebang. There are links in the slides that show you how to install Windows Subsystem for Linux, and also how file association in Windows work, and you can follow these links to learn more about these topics if interested. Great!

03:46 By now, you should have a fair understanding of what the shebang is and why it’s important. In the coming lessons, you get into more practical exploration of the shebang.

Become a Member to join the conversation.