Python for Developers — 100daysoflearning (PART 8)

Saiyam Pathak
100daysoflearning
Published in
4 min readApr 7, 2019

Day 68–70

In continuation to the course for Python For Developers lets move on with next set of learning topics.

Environment Variables :
As the name suggests environment variables help to configure the environment for the program to run with specific set of values which are set as environment variables.

In order to use environment variables you need to import the os package. Operating system module comes with lot of different set of objects but today I will be talking about ‘os.environ’ and ‘os.getenv’.
os.environ behaves like a dictionary and subscript is used to read from it. Lets see an example usage for it .

environ.py

The above program expects STAGE as a variable to be passed or set before running the program as its a dictionary and if id doesn't find a value for the key it will return a error. Below is the output for the program.

output

you can see the error if no value is passed for STAGE.

In order to remove the above issue I will use ‘os.getenv’ which handles the missing environment variable with a default value.

getenv

getenv takes the default argument as well which means if you do not give the value for the key STAGE then it will automatically take the default value it. dev in this case.

This is how the Environment variables can be used in a program to provide the value at run-time and one can set default as well to avoid any errors if no value is found for the key mentioned in the program.

Error Handling : Now in any program that you write things might not go as expected and there are scenarios where the program ends up in an error and the execution of the program is interrupted and not completed. For such cases where you can suspect an error or you want to handle different types of error so that the execution of the program does not get halted , use Error Handling. In this section I will tell you about error handling with try/except/else/finally. Lets first try to write to a file that do not exist and see the error .

myerror.py with output

As you can see that the program runs the first time as the file is not present that time but when you try to run it again it says FileExistsError meaning that a file is already there. Now to overcome such scenarios you need to use try and except and refactor the program .

You can also create multiple except for specific type of errors:

Above shows multiple Excepts with more specific errors.

Two more things- else and Finally , else means what to run of there is no error and finally block will run every-time just you need to remove the ‘sys.exit’ . Lets refactor the code and make it more sensible using error handling.

Now you can see how try, multiple except , else and finally can be used in order to make program execution more smooth and more robust.

In this tutorial we have discussed about using Environment variables and Error handling in Python.

Happy Learning & Happy Coding
Saiyam Pathak
https://www.linkedin.com/in/saiyam-pathak-97685a64/
https://twitter.com/SaiyamPathak

--

--

Saiyam Pathak
100daysoflearning

l CNCF Ambassador | CKA | CKAD | Influx ACE | Multi-cloud certified | Rancher Ranch Hands member