Following Shebang Best Practices
00:00 Let’s look at some of the best practices and things to take note of when using the shebang. First of which is that the shebang is only applicable to executable scripts on Unix-like operating systems, that is, Linux or macOS.
00:13 Without a shebang in a script, your shell will attempt to interpret the script as if it were written in the corresponding shell language. Shebangs are not to be used in plain Python modules that are only meant to be imported, not executed directly.
00:29 If you are to use the shebang, ensure that you begin your script with the shebang line and do not place any other comment before it.
00:37 You should start the shebang with the hash symbol and an exclamation mark to distinguish it from standard comments.
00:44
You should use the /usr/bin/env
command to avoid hard coding an absolute path to any specific Python interpreter. This way, you define what is called a portable shebang.
00:58
If you have multiple Python interpreters on your system, that is Python 2 and Python 3, then you should avoid using the bare python
command and be more specific or explicit with python3
.
01:09
And finally, enable the -s
flag if you need to pass extra arguments to the interpreter.
01:16 In this lesson, you’ve learned more about the use cases and best practices for the shebang. You’ll now wrap things up in the next lesson.
Become a Member to join the conversation.