Simple Programs
Simple Programs with Python
In this session we will look at writing some simple stand alone programs in Python. We will cover the following topics:
#!/
__main__
sys.argv
The first thing to note is that Python is an interpreted language, this means that you can run Python code directly from the command line. This is done by typing python
followed by the name of the file containing the Python code. For example to run a file called hello.py
you would type:
mkdir simple_programs
cd simple_programs
touch hello.py
code hello.py
This will open the file in the VSCode editor and you can then type in the following code:
print("Hello World")
python hello.py
This should print out Hello World
to the console. If we type the following:
ls -al
total 72
drwxr-xr-x. 2 jmacey cgstaff 4096 Sep 19 10:15 .
drwx------. 284 jmacey cgstaff 65536 Sep 19 10:16 ..
-rw-r--r--. 1 jmacey cgstaff 0 Sep 19 10:15 hello.py
You will notice that the file is not executable, this is because we have not set the execute bit on the file. We can do this by using chmod which is a command line utility to change the permissions of a file. To make the file executable we type:
chmod +x hello.py
This adds the execute bit to the file, if we now type:
ls -al
total 72
drwxr-xr-x. 2 jmacey cgstaff 4096 Sep 19 10:17 .
drwx------. 284 jmacey cgstaff 65536 Sep 19 10:17 ..
-rwxr-xr-x. 1 jmacey cgstaff 21 Sep 19 10:17 hello.py
You will notice that the file is now executable and has turned green in the terminal. We can now run the file by typing:
./hello.py
./hello.py: line 1: syntax error near unexpected token `"hello world"'
./hello.py: line 1: `print("hello world")'
As you can see this gives a lot of error messages, this is because the shell does not know how to run a Python file. To fix this we need to add a special line to the top of the file. This is called the shebang line and is used to tell the shell what program to use to run the file. In the case of Python we use:
#!/usr/bin/env python
print("Hello World")
#!
This is the first line of the script and informs the shell which interpreter to use, this can be any scripting language and there are a few variants we can use, but we usually use either
#!/usr/bin/env python
or
#!/usr/bin/python
If you have several versions of Python installed /usr/bin/env
will ensure the interpreter used is the first one on your environment $PATH
, it is also possible to hard code paths to specific versions of Python but this is not recommended as it can cause issues if the script is run on a different system, however if you need to use something like mayapy then you would use:
#!/usr/autodesk/maya/bin/mayapy
main
In many programming languages the main function is the entry point of a program, this function additionally may have command line arguments passed to it to allow the programs behavior to be modified (for example ls vs ls -al), typically main is used to do the initial setup of a stand alone program before executing the overall program structure / processing loop.
main
Anything in python that begins with a __
is know as a “dunder” or double underscore. The __main__
is a special variable in Python that is used to determine if the script is being run as the main program or if it is being imported as a module. This is useful as it allows you to write code that can be run as a standalone program or as a module that can be imported into another program.
In python to use the __main__
variable we use the following code:
#!/usr/bin/env python
def main():
print("Hello World")
if __name__ == "__main__":
main()
This code defines a function called main
that prints out Hello World
, it then checks if the script is being run as the main program and if it is it calls the main
function. This is useful as it allows you to write code that can be run as a standalone program or as a module that can be imported into another program (more on this in another lab).
sys.argv
The sys.argv
variable is a list in Python that contains the command line arguments passed to the script. The first element of the list is the name of the script and the remaining elements are the arguments passed to the script. For example if we have a script called args.py
that contains the following code:
#!/usr/bin/env python
import sys
def main():
print(sys.argv)
if __name__ == "__main__":
main()
If we run this script with the following command:
./args.py -al hello what does this do
['./args.py', '-al', 'hello', 'what', 'does', 'this', 'do']
You can see that the first element of the list is the name of the script and the remaining elements are the arguments passed to the script. This is useful as it allows you to write scripts that can take command line arguments to modify their behavior.
What next
In the next session we will be looking at how we can use the sys.argv
variable to write a simple program that can take command line arguments to modify its behavior. After that we will look at some more complex modules that make the work simpler for us.
The data type of sys.argv
is list of strings representing the arguments. Knowing this think how you would do the following :
- count the number of arguments passed to the script
- print out the arguments passed to the script
- determinate if a specific argument was passed to the script (for example -a)