What I am doing currently
I use PyCharm's new project setup wizard to create Django projects. I set a virtualenv. In the virtualenv's activate.bat and deactivate.bat, I edit those manually to set environment variables for Django's settings.py : secret key, postgresql database name, postgresql database user, postgresql database password variables. For example, I am pulling these values out of the environment variables - 'NAME': os.environ['DATABASE_NAME'].
This approach works but whenever I open a project, the terminal conveniently activates the virtualenv (which I like) but doesn't load the environment variables so I just deactivate and activate again and they are loaded. Not a big deal but I'm wondering if I can do this without editing the venv activate.bat and deactivate.bat files and just use PyCharm's ability to set environment variables.
What I have tried
I'm not trying to set environment variables explicitly in the terminal : PyCharm, Virtualenv and Environment variables
I tried "Edit configurations" > Environment > Environment variables > ... > Name=DJANGO_SECRET_KEY, Value=<the random string with no quotes or anything> > Apply button > OK > in Terminal : How to set environment variables in PyCharm?
(venv) C:\Users\jarad.collier\PycharmProjects\tdm>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 15, in <module>
File "C:\Users\jarad.collier\PycharmProjects\tdm\venv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
File "C:\Users\jarad.collier\PycharmProjects\tdm\venv\lib\site-packages\django\core\management\__init__.py", line 317, in execute
File "C:\Users\jarad.collier\PycharmProjects\tdm\venv\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
File "C:\Users\jarad.collier\PycharmProjects\tdm\venv\lib\site-packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\jarad.collier\PycharmProjects\tdm\venv\lib\site-packages\django\conf\__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\jarad.collier\PycharmProjects\tdm\tdm\settings.py", line 23, in <module>
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
File "C:\python36\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
Since that didn't work, I tried various other methods illustrated here: Pycharm: set environment variable for run manage.py Task
Take-aways from that are that I can set environment variables in several places!
- File > Settings > Build, Execution, Deployment > Console > Python Console or Django Console
- File > Settings > Languages & Frameworks > Django > Manage.py tasks
- Edit configurations area (already mentioned)
What I want to be able to do
Very specifically, I want to set environment variables within PyCharm to hide secret things (secret key, database info) in the environment itself. Then I want to be able to run "python manage.py runserver" very specifically within the "Terminal" that's inside PyCharm and have it run without throwing KeyError (because it finds the variables).
Where and how within PyCharm can I do this?
Note: I'm not really interested in alternative methods like storing an external file somewhere or having an external script set these variables. I REALLY want to be able to do this within PyCharm and to understand the process and what I'm doing wrong!