Categories
Python Answers

How to set environment variables in Python?

Spread the love

Sometimes, we want to set environment variables in Python.

In this article, we’ll look at how to set environment variables in Python.

How to set environment variables in Python?

To set environment variables in Python, we can put an entry the os.environ dictionary.

For instance, we write:

import os

os.environ["DEBUG"] = "1"
print(os.environ["DEBUG"])

to set the DEBUG environment variable to '1'.

And to get the value of the DEBUG environment variable, we write:

print(os.environ["DEBUG"])

and we should see '1' printed.

Conclusion

To set environment variables in Python, we can put an entry the os.environ dictionary.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *