Ubuntu environment variables and accessing them via python

Rupesh Desai
Python Bootcamp Basics
3 min readJul 23, 2021

It is quite a daunting task to figure out how to set environment variables and access them in your application until you know exactly how to do it.

My goal in this story is to provide a short two-minute read-up / video on achieving this quickly.

There are two types of commands on Linux / Ubuntu to set environment variables if you are reading this.

  1. Temporary Environment Variables: You can set the environment variable right from the bash/terminal screen. You would lose them as soon as you log out or restart the system. These are perfect if your variable values change periodically and you need to set and reset them often.
  2. Permanent Environment Variable: These are persistent key-value pairs that you can access on an ongoing basis. I use authentication codes, API keys, or any other obfuscated keys that I don’t want to store in my code and put them in public places like GitHub, even if it is a private repository.

Let’s get to do how to set environment variables now.

To set a temporary environment variable, simply use the export command.

$ export <variable name> = <value>
For example:
$ export mysecretcode=ITSASECRET

Of course, in this case, the variable name is “mysecretcode” and the value I will retrieve is “ITSASECRET”. To try accessing this value from the terminal. Just simply do:

$ echo "$<variable name>"
# In this case
$ echo "$mysecretcode"

Make sure that you add the $ sign before the variable name. You should see the result output.

$ ITSASECRET

Now to access this variable in python. So long as you have not terminated the session in the terminal, or restarted your computer. You can do the following.

import osprint(os.environ['mysecretcode'])

Yes, you can have multiple, environment variables called by adding it to the list. Ok, now let’s move on to setting up the variable permanently on your machine. To do this you will have to edit the .bashrc file on your machine. Note, this will add the variable globally.

$sudo nano ~/.bashrc
$[sudo] password for yourmachine:

You will need to enter the password, and the bash file should open up in your terminal. Once the file opens, you can simply go to the end of the file through your downward arrow key and write your environment variable, but remember, do not add the keyword “export” when adding the variable. Simply the variable name and the key in quotes as shown below.

<variable name>="key"
mysecretcode="ITSASECRET"

Finally, do CTRL+X or CMD+X(if on Mac), To exit, it will ask you to save the changes (SHIFT + Y), then hit enter. Please note that changes wouldn’t take place until you restart the terminal. Hence exit out and restart. Wallah… this should do the trick, now your permanent key is set and can be accessed as an environment variable.

Hope this tutorial was helpful. Please send me your comments or feedback.

--

--

Rupesh Desai
Python Bootcamp Basics

Blogger, Entrepreneur, Data Analyst, Developer, Designer, Linear Creative, FitnessFan