I have a problem with virtualenvwrapper
on ubuntu.
I'm working on simple Django project foo
. I've decided to move SECRET_KEY
from settings.py
and save it as environment variable. Everything went well:
In bash I've entered
export SECRET_KET=['...']
In settings.py
I've entered:
SECRET_KEY = os.environ['SECRET_KEY']
Also I tested that app is working and everything was ok.
BUT
After I've started working on project once again using workon foo
command in bash and I've tried to simply run server with python manage.py runserver
and the SECRET_KEY
is not working. The error I get is:
...
SECRET_KEY = os.environ['SECRET_KEY']
File "/home/user/.virtualenvs/foo/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
I've read that this variable should be set in postactivate
file in .virtualenvs/foo/bin/
, but there is nothing there.
Fortunately it was just simple test project, but how can I be sure that environment variable will be saved in my next virtualenv when using virtualenvwrapper
?
the export
command marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables but does not persist it. If you want the variable to be permanently available in your virtual environment you have to add it manually to the postactivate
script:
echo 'export SECRET_KET=KJGH768&^jhgJg' >> /home/user/.virtualenvs/foo/bin/postactivate
But also you have to unset the variable once you leave the virtualenv to avoid any conflicts as follow:
echo 'unset SECRET_KET' >> /home/user/.virtualenvs/foo/bin/predeactivate