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:
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.
requirements.txt
, containing the one line python-dotenv==1.0.1
.Python Setup App
i cPanel, I added the requirement.txt as a config file.Run Pip Install
. Run this..env
, and add a line DB_EMAIL_PASSWORD_V=MySuperSecretPassord
Finally, I could run the following:
import os
from dotenv import load_dotenv
load_dotenv()
print(os.environ.get('DB_EMAIL_PASSWORD_V'))