Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How to set environment variables in Python

Environment variables are variables that are set in the operating system and can be accessed by different programs. They are useful for storing configuration values or other information that needs to be shared between different applications. In Python, we can set environment variables using the os module. In this blog post, we will discuss different ways to set environment variables in Python.

Before we dive into setting environment variables, let's briefly review the os module. This module provides a way to interact with the operating system in a platform-independent manner. It allows us to perform various system-level operations such as reading and writing environment variables, getting information about the operating system, and much more. Once you import the os module, there are two ways to set environment variables.

In the examples here we will show you how to set some dummy environment variables that will be available for you to use in your program but the same logic applies to any environment variable that you wish to update.

Method 1: Using the os.environ dictionary

Here is the simplest The os module provides a dictionary-like object called environ that contains the current environment variables as key-value pairs. We can use this dictionary to set environment variables as follows:

import os

os.environ['SCHOOL'] = 'Kodeclik'

Here we are defining an environment variable called ‘SCHOOL’ which is set to value ‘Kodeclik’. All values should be strings.

Note that changes made to the environ dictionary will only affect the current process and its child processes.

After the above code, if we do:

print(os.getenv('SCHOOL'))

we will get:

Python how to set environment variables

Kodeclik

as intended.

Method 2: Using the os.putenv() function

The os module also provides a function called putenv() that can be used to set environment variables. This function takes two arguments: the name of the environment variable and its value.

The equivalent code using the putenv() function is:

import os

os.putenv('SCHOOL','Kodeclik')
print(os.getenv('SCHOOL'))

with corresponding output:

None

Woah - what happened? According to the Python documentation, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ directly. So the change happening after putenv() is called applies to the child processes but not quite to the current process (which is why when we attempt to print it, we get a None).

In this blog post, we discussed two ways to set environment variables in Python. We used the os.environ dictionary to set environment variables as key-value pairs, and we used the os.putenv() function to set the value of a specific environment variable. Both methods are valid but calling os.putenv() directly does not change os.environ, so it’s better to modify os.environ according to our first method.

If you liked learning how to set environment variables, learn how to access the values of environment variables. Also learn more about finding parent directories in Python using the os.path module!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.