0

today I spent hours trying to use Environment Variables in Debian for a Python project. I tried exporting them in various files:

/home/user/.profile
/home/user/.bashrc
/etc/environment
/etc/profile
/etc/profile.d/
/root/.profile
/root/.bashrc

I kept getting KeyError errors when trying to retrieve them in my settings.py file of Django. Finally, I found another post suggesting to declare them in a file under system.d and it finally worked.

However I wanted to try to set a config.ini file in my project folder where I would place those Environment Variables, and parse it by importing configparser inside Django's settings.py. I thought maybe it would be safer than EVs, if I set the file only readable by the user executing the process. But I get the exact same KeyError errors than when I tried exporting EVs:

Sep 20 11:44:06 main gunicorn[1544]:     SECRET_KEY = config['secret']['key']
Sep 20 11:44:06 main gunicorn[1544]:   File "/usr/lib/python3.9/configparser.py", line 960, in __getitem__
Sep 20 11:44:06 main gunicorn[1544]:     raise KeyError(key)
Sep 20 11:44:06 main gunicorn[1544]: KeyError: 'secret'

In top, Gunicorn process is run by my sudouser. And chmod 777 or chown root:root doesn't solve the problem. I don't understand why Gunicorn can't access the config.ini file located in my project folder, next to settings.py. I can access those keys when using the Python shell. It just seems the same exact problem than with the Environment Variables but I don't understand why it does not work. I am using Debian 11 on a Droplet. Any idea?

Thank you for your help.

Anthony
  • 1
  • 1

0 Answers0