1

I have an environment variable exported in bash. Project is in localhost. OS is Debian 9 Stretch.

When I run, in command line:

python -c "import os; print(os.environ.get('EMAIL_PASSWORD'))"

I get the variable value.

But, inside Django, calling os.environ.get('EMAIL_PASSWORD') returns None.

In settings:

EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')

Already find n similar questions, but in general the problem is that the variable wasn't exported to environment.

What I am missing?

Update: Django virtual server is called directly from Pycharm

Caco
  • 1,601
  • 1
  • 26
  • 53
  • where is you environment variables set?? – Exprator Jul 03 '17 at 11:52
  • I'm setting it with `EMAIL_PASSWORD=password` and then exporting with `export $EMAIL_PASSWORD`. `printenv` returns it. – Caco Jul 03 '17 at 12:04
  • are you putting them in .bashrc file or .env file? – Exprator Jul 03 '17 at 12:07
  • And how are you starting Django? – Daniel Roseman Jul 03 '17 at 12:11
  • @Exprator, by now I'm only exporting in current environment not in `.bashrc`, but `printenv` displays that. Starting Django with `python manage.py runserver 8000` – Caco Jul 03 '17 at 12:43
  • There's no global environment. Variables must be exported from the parent shell before you start a child shell or child process. How and when did you export the variable? Have you exported the variable in the parent shell before starting runserver? [mcve] – Håken Lid Jul 03 '17 at 13:11
  • Thanks @HåkenLid. It was missing the important information that I'm running Django virtual server from Pycharm. Pycharm doesn't know about environment variables from other shells. So that was the problem. I've been configure the environment variable in PyCharm Run/Debug Configurations -> "Environment variables", so now it works. Related question: https://stackoverflow.com/questions/21538859/pycharm-set-environment-variable-for-run-manage-py-task#21619127 – Caco Jul 04 '17 at 10:37

1 Answers1

0

export $EMAIL_PASSWORD will not work. First remove the $ and define the variable value in the shell, not only the variable name. It should look like this: export EMAIL_PASSWORD='value'

Edit

Don't put the ''around the var when you call it. Django will think its a string, not a variable.