Predictive Hacks

Working with Environment Variables in Python

A common way to configure scripts is to use environment variables, like the credentials and so on. The os package enables us to interact with the environment variables.

How you can get all the environment variables

import os
os.environ

How we can create a new environment variable

os.environ['MyBlogPassword'] = 'MyPassword'

How to get an environment variable

os.environ.get('MyBlogPassword', 'give_your_default_value')

How to remove an environment variable

# using pop
os.environ.pop("MyBlogPassword")

# using del
del os.environ["MyBlogPassword"]

Working with Environment Variables in a Conda Environment

If you are working in a conda virtual environment, you can create environment variables as follows:

conda env config vars set my_var=value

Once you have set the environment variables, you will need to reactivate the environment by running:

conda activate env_name

Finally, if you want to check if the environment variable has been set, you can run:

echo my_var

Or

conda env config vars list

The latter returns a list of any variable you may have set.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s