Advertisements

How to set environment variables for your Python application from PyCharm

The Twelve-Factor App methodology recommends storing configurations in the environment.

When you follow this methodology and want to run your Python application from PyCharm, you need to create a customised run configuration.

In case you need it, this post shows how to set environment variables for your Python application from PyCharm.

Creating a run configuration with environment variables

To create a run configuration with environment variables, you can follow through these screens:

While your project is selected, click on Run -> Edit Configurations...

PyCharm CE Version 2018.2.3 Selecting Edit Configurations... from Run Menu

Give your run configuration a name and click on the folder icon in the Environment Variables text input:

PyCharm CE Version 2018.2.3 Run Debug Configurations screen

Click + to add a new environment variable. When you had create all the environment variables that you wish to supply to your Python application, click OK.

PyCharm CE Version 2018.2.3 Environment Variables Dialog with 2 environment variables

Click Apply then click OK to finalize your run configurations
PyCharm CE Version 2018.2.3 Run Debug Configurations screen with 2 environment variables set

Running or debugging your Python application with the new run configuration containing your environment variables

Once you had created your run configurations with the environment variables that you want to feed your application, you can right click on a Python script to run or debug it:

PyCharm CE Version 2018.2.3 editor with new run configurations for running and debugging a script

When you run or debug with the new run configurations, your Python application will be able to access the environment variables that you had created:

PyCharm CE Version 2018.2.3 editor after running a run configuration with an environment variable MESSAGE=Hello World

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

Advertisements
Advertisements

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap