Python Tips: What The #! Is This?
As you start to look at other Python scripts, you will likely see a line like this at the very top:
#!/usr/bin/python
At first glance, you may think this is a line that's been commented out because it begins with a hash (#) character. But, in fact, that is not a comment because the hash character is followed by the exclamation point and that combination has a unique purpose.
What does #! mean?
In Linux, the ‘!’ character is often referred to as ‘bang’ so the sequence of hash-bang is sometimes referred to as ‘shebang’. What follows the shebang is usually a path. That combination of the shebang and a path tells the operating system (Linux) which interpreter should be used to run a script. So, in reality, it’s not so much a Python thing as it is a Linux thing.
Wait. What’s an interpreter?
In order to make sense of an interpreter, it makes sense to contrast an interpreter to a compiler. All programming is about giving a computer a series of steps we need it to perform. Computers cannot understand human languages, only machine language (which boils down to ones and zeros but we won’t go into that today). By the same token, humans aren’t very good at speaking machine language so we use high-level languages that can be converted to machine language. How the high-level language commands (i.e. scripts) are converted to machine language depends on the language. Some languages use compilers and some use interpreters.
Now compilers vs interpreters!? I’m so confused!!
Let’s try a more human example. Imagine you want to go to China but, like me, you don’t know how to speak the local language. You know you have no hope of being able to order in a restaurant but you know you will have to eat at some point so you need a way to make yourself understood to the wait staff. There are two ways you can go about this:
- When you get to China, you find a translator and you hand them a document that has your order on it. The translator takes your document, writes an equivalent document in the local language and hands that new document back to you. From that point forward, you take that document with you whenever you want to eat. You enter the restaurant and, when the wait staff comes to take your order, you just hand them the document containing your entire order. They go get your food and you are happy and fed for another day. But what if you get tired of eating the same thing every day? Well, you could go back to the translator and have them translate another document with another order or you could go with option 2.
- When you get to China, you hire an interpreter. You take that interpreter with you each time you want to eat. When the wait staff asks for your order, you turn to the interpreter and tell them one item on your order. The interpreter tells the wait staff what you want and they go get it for you. This process repeats itself, one item at a time, until you are happy and fed for another day.
You can see that both options have their pros and cons but they both work. Which option is best really depends on what you need. For example, you wouldn’t use an interpreter for a dissertation; that’s a job better suited for the translator. By the same token, compiled languages versus interpreted languages have their pros and cons and which one you use depends on the task you want the computer to perform.
So what?
All that discussion was just explain what an interpreter is. The main thing you need to remember is that Python is an interpreted language and that’s why that first line in your script is so important.
How do I use it?
When you are writing a Python script, make sure you have a line like this at the very top:
#!/usr/bin/python3
If you’re not using Python version 3, you can probably get by with this:
#!/usr/bin/python
If you’re not sure which version you have, you could use this line instead:
#!/usr/bin/env python
This command runs the env
executable and tells it to search the $PATH environment variable for the string “python”. This should return the most recently installed version of Python.
Conclusion
The shebang is your friend. Without this line your script, you would always have to include the interpreter name on the command line when you want to run your script like this:
% python3 <script_name>
Also remember that the shebang is a requirement for the operating system, not the scripting language. For example, BASH scripts often start with this line:
#!/bin/bash
At any rate, you only need that line once in your script and it must the very first line. Happy scripting!