You are currently viewing Using #!/usr/bin/env on the first line of a Python script

What is the Usage of writing #!/usr/bin/env on the first line of a Python script? It is always the best practice to write this statement as the first line while writing the Python script, this line is called “shebang” or “hashbang”. It is used to tell the system which interpreter to use for running the script.

Advertisements

Many beginners might not know what it means and why it is used. In this article, we will see the importance and usage of this shebang with examples.

1. What is “#!/usr/bin/env” in the First Line of Python?

Write “#!/usr/bin/env” as the first line in the Python program/script that is used to tell the system which interpreter to use, this special line is called “shebang” or “hashbang”.

This first line sounds strange in the python script as this looks like a comment. Let’s take a look at the examples of what happens when you have this statement and when not.

This is the code from the first file “file_1.py” without this special line.


# With out "shebang" or "hashbang" line
print('hello world')

This is the code from the second file “file_2.py” with special line.


# !/usr/bin/env python
# With "shebang" or "hashbang" line
print('hello world')

If you run these two files from the editor or terminal both will yield the same result. So what is the catch? Let me explain it to you.

1.1 Without “shebang” or “hashbang” Line

The code in the first file file_1.py is what we normally do. To execute this program you have to run it like the below. However, we can’t run this program from all operation system versions.


# Execute python script
python file_1.py

1.2 With “shebang” or “hashbang” Line

To write code for different users for distribution or for the different versions of python, we need to be clear. We have to tell the operating system what interpreter should our code be interpreted in.

The first line on the file_2.py is used to tell the system which interpreter to use for running the script.

If you include the shebang line at the beginning of the script, you can run it directly from the command line by using the below command:


# Execute the python script
./file_2.py

2. Running Scripts with Different Versions of Python

Let’s say you are having multiple versions of python installed on your system. You write the python code for a specific version of python. You will need to specify the python version each time during the execution otherwise you will get errors. This is where the main usage of !/usr/bin/env comes.

This means that it will make your script less portable since the user running the script will need to know the path to the Python interpreter on their system. This can also lead to errors if you are using a virtual environment in Python.

Let’s say your script is specifically designed to be run on python version 3.4 the first line of your python code should be like this:


# !/usr/bin/python3.4
# Your code Below
print('Hello World')

This is specifically useful when you are using python version 3 and python version 2. For the code to be run on python version 3, you have to write it as follows:


# !/usr/bin/env python3
# Your code below

And for the default version of python, which in most systems is python version 2, you can write it like this.


# !/usr/bin/env python
# Your code Here

Remember, if the default version of python is 3, then it will run on that specific version.

3. Setting Script Execution Permissions

The shebang can also be used to set the execute permissions for a Python script. By including the shebang line at the beginning of the script and then using the chmod command to set the file permissions to the executable, you can run the script directly from the command line.


# !/usr/bin/env python
# Code Here

This shebang line is used in combination with the chmod command to set the file permissions to executable. For example, if your script is called app.py, you can run the following command to set the permissions and make the script executable:


chmod +x app.py

4. #!python vs #!/usr/bin/env

In many cases both #!python and #!/usr/bin/env python are used to specify the path to the Python interpreter that should be used to run a Python script on the first line.

Windows has a default behavior of using a default Python interpreter when no shebang is present. In this case, #!python could be used to explicitly specify the version of Python to use.

However, if you are running your script on a bash, then it is a good idea to use the #!/usr/bin/env instead of #!python.

5. Summary and Conclusion

In the article, you have learned what is the usage of writing #!/usr/bin/env on the first line of a Python script, we have seen using both #!python , #!/usr/bin/env and other alternatives of this shebang. I hope this article was helpful. If you have any questions, please leave them in the comment section.

Happy Coding!

Related Article