DEV Community

CincyBC
CincyBC

Posted on

2 2

Environment Variables in Python

Environment variables are crucial in development so you don't accidentally commit keys/passwords up to a repo. Web developers in JavaScript will process.env to access their environment variables.

The cleanest way I've figured to do this is Python is to utilize an Anaconda environment and setting them there accessing them in code with os.environ.

Here is a quick example from your terminal:

conda create -n test-env
conda activate test-env
conda env config vars set my_var=value
conda activate test-env
Enter fullscreen mode Exit fullscreen mode

Once you have set your variable, you have activate your environment again as above.

To check if it worked, you can either echo my_var to check it specifically or conda env config vars list to see a list of them all.

If you want to reuse an environment, you can use a shell script to set and unset your variables upon deactivation. See docs for how to do it in Windows (with a .bat file) and Mac/Linux.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay