djangoenvironment-variablescpanelenvironmentproduction-environment

How to use Django - cPanel environment variable


While setting up a Django app in cPanel, I can assign environment variables. However, I am having trouble using them. For instance, I made an assignment as seen in the picture. But when I use it in my Django application as os.environ.get('EMAIL_PASSWORD_V'), It doesn't recognize the variable. How can i fix that?

I know there are other methods to do this, but if this method is can be used, I wonder how can it.

Django_Variable_in_cPanel:

Django_Variable_in_cPanel


Solution

  • I had the same problem. I set environment variables in CPanel, under Setup Python App, but was not able to locate the variables in my program. Neither in os.environ, nor in the environment object passed in to the application() function. :-/

    My hosting provider recommended me using python-dotenv (https://pypi.org/project/python-dotenv/).

    EDIT: Sorry, @Bedirhandd. My hosting provider were not able to help. It was outside their expertise, unfortunately. To me, it looks like a problem with cPanel itself. I looped over all the environment variables in os.environ, and all the variables in the environment object that was passed to my application. None of my custom environment variables that I created in cPanel were present anywhere.

    I did get it environment variables to work using python-dotenv, though.

    Finally, I could run the following:

    import os
    from dotenv import load_dotenv 
    
    load_dotenv() 
    print(os.environ.get('DB_EMAIL_PASSWORD_V'))