Under a Unix-like operating system such as Linux, Python program files can be made directly executable using a shebang, also called a magic line. To do this, the first line of the program file must usually be as follows:
In this case, the operating system is instructed to always execute this program file with the Python interpreter. On other operating systems, such as Windows, the shebang line is ignored. Note that the Python interpreter may be installed on your system in a different directory than the one specified here. Generally, therefore, the following Shebang line is better, since it is independent of the actual installation location of Python:
Also note that the executable flag of the program file must be set before the file is actually executable. This is done with the command
The examples shown in this book do not include a shebang line for clarity. However, this does not mean that the use of a Shebang line is inadvisable.
Happy Coding!
Code:
#!/usr/bin/python3
In this case, the operating system is instructed to always execute this program file with the Python interpreter. On other operating systems, such as Windows, the shebang line is ignored. Note that the Python interpreter may be installed on your system in a different directory than the one specified here. Generally, therefore, the following Shebang line is better, since it is independent of the actual installation location of Python:
Code:
#!/usr/bin/env python3
Also note that the executable flag of the program file must be set before the file is actually executable. This is done with the command
Code:
chmod +x filename
The examples shown in this book do not include a shebang line for clarity. However, this does not mean that the use of a Shebang line is inadvisable.
Happy Coding!