Setting DJANGO_SETTINGS_MODULE under virtualenv?
Asked Answered
T

2

13

I want the environment variable DJANGO_SETTINGS_MODULE to change depending on what I say for workon. It seemed to me that I want to set it in .virtualenvs/postmkvirtualenv but my trial had no effect.

ftpmaint@millstone:~$ cat ~/.virtualenvs/postmkvirtualenv
#!/bin/bash
# This hook is run after a new virtualenv is activated.
export DJANGO_SETTINGS_MODULE=newproject.settings
ftpmaint@millstone:~$ echo $DJANGO_SETTINGS_MODULE
az.settings
ftpmaint@millstone:~$ workon newproject
(newproject)ftpmaint@millstone:~$ echo $DJANGO_SETTINGS_MODULE
az.settings

Could someone set me straight; where should I put that export?

In addition, will it restore when I deactivate? And if not, is there some natural way to restore it?

Tonsorial answered 29/9, 2011 at 14:24 Comment(1)
You can do this: #8827034Poi
S
14

You were on the right track, but you want to use the postactivate hook instead of postmkvirtualenv.

It won't restore automatically on deactivate. Thankfully there is postdeactivate hook that you can use to manually restore any environmental variables that you changed on activate.

Sherd answered 29/9, 2011 at 15:7 Comment(3)
Thank you (to both answerers). The key was "activate", and I didn't understand that before.Tonsorial
Isn't the postactivate hook common for all environments? I think there is not much point of setting DJANGO_SETTINGS_MODULE there.Muntin
There's both a global and local version of the postactivate hook. For a local-only setting like this, you'd want to add the export to $VIRTUAL_ENV/bin/postactivate rather than the global $VIRTUALENVWRAPPER_HOOK_DIR/postactivate.Sherd
H
22

One way I've done that before is by appending an export statement to the end of ./bin/activate

export DJANGO_SETTINGS_MODULE="myproject.settings"
Haletky answered 29/9, 2011 at 14:43 Comment(0)
S
14

You were on the right track, but you want to use the postactivate hook instead of postmkvirtualenv.

It won't restore automatically on deactivate. Thankfully there is postdeactivate hook that you can use to manually restore any environmental variables that you changed on activate.

Sherd answered 29/9, 2011 at 15:7 Comment(3)
Thank you (to both answerers). The key was "activate", and I didn't understand that before.Tonsorial
Isn't the postactivate hook common for all environments? I think there is not much point of setting DJANGO_SETTINGS_MODULE there.Muntin
There's both a global and local version of the postactivate hook. For a local-only setting like this, you'd want to add the export to $VIRTUAL_ENV/bin/postactivate rather than the global $VIRTUALENVWRAPPER_HOOK_DIR/postactivate.Sherd

© 2022 - 2024 — McMap. All rights reserved.