Learning About Standalone Programs
00:00 In the last lesson, you saw the importance of the shebang in an executable script. In this lesson, you’ll learn what types of Python scripts benefit the most from using the shebang.
00:10 Typically, shebang are used in Python modules that are meant to be run directly from the command line. That is, modules that are intended to be executed as standalone programs.
00:21 Standalone programs contain all the necessary code, that is, function definitions and all required inputs to perform its intended operation. And now you create an example standalone program and use the shebang with it.
00:34
You can create a file you call shebang_example.py
. In
00:41
here, I’ll add my shebang up top: #! /usr/
bin/python3
.
00:52
For this example, you can have a basic add()
function, that is, say def add()
. This function will take two arguments, num_1
and num_2
.
01:01 You can always add a docstring.
01:06
This function adds two integers together, and then you just return the result of the operation num_1 + num_2
.
01:16
And in this same module, I’ll utilize and call this add()
function. This, I’ll just define the main()
function. I’ll call and display the result of this add()
function, say def main()
.
01:27
This function will take no argument and to a variable called add_result
.
01:32
I’ll assign the value of calling add()
function with arguments 5
and 7
. To see the results using print()
function using an f-string, the main add result is the add_result
variable.
01:47
This is a basic Python program that calls an add()
function through a main()
function and prints out the result of the operation in the terminal. In Python, when creating executable scripts, you should add the if __name__ == "__main__":
02:04
With this if __name__ == "__main__":
conditional block, functions you define or invoke in this block will be called only when the module is run directly as a script, and it prevents them from running when someone imports this file or some of its content from another module.
02:19
So in here, I can call my main()
function. This is an example of a standalone program because it has all it needs to carry out the intended operation.
02:28 So it means this is a good candidate for using the shebang. Adding a shebang here can be used to tell the shell which interpreter to use when running the standalone Python program.
02:38 For modules that only contain functions to be imported into other modules, the shebang wouldn’t be needed because the module is not intended to be executed as a standalone script.
02:48
If you save your script, you can head over to your terminal to give this a try. You’d normally call this script as with any other Python script as python3
examples/shebang_example.py
.
03:02 But since you have your shebang in your script, you can start to make use of it with the shebang. As long as you have an executable script, you should be able to call the script directly.
03:13
But if you remember from previous lessons, by default, your scripts are not executable. And to make a script executable, you use the chmod +x
command.
03:24
So to your examples/shebang_example.py
, you call the command chmod +x
.
03:31 And after making the script executable, you can then call your script and you should get the results. The main add result is 12. This works because your shell has access to the Python interpreter specified by this absolute path in the shebang.
03:47 However, sometimes the absolute path to the Python interpreter may vary in different people’s machines or run environment, which is an issue sometimes faced when using the shebang with absolute path. And the solution to this, you’ll get to see soon.
04:02 You now know what types of scripts the shebang is best suited for. In the next lesson, you’ll learn about flexible and portable shebang.
Become a Member to join the conversation.