在 virtualenv 下设置 DJANGO_SETTINGS_MODULE?
我希望环境变量 DJANGO_SETTINGS_MODULE 根据我对 workon 所说的内容进行更改。在我看来,我想将其设置在 .virtualenvs/postmkvirtualenv
中,但我的尝试没有效果。
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
有人可以纠正我吗?我应该将该导出放在哪里?
另外,停用后会恢复吗?如果没有,是否有一些自然的方法可以恢复它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前这样做的一种方法是将导出语句附加到 ./bin/activate 的末尾
One way I've done that before is by appending an export statement to the end of ./bin/activate
您的方向是正确的,但您想要使用
postactivate
钩子而不是postmkvirtualenv
。停用后不会自动恢复。值得庆幸的是,有
postdeactivate
挂钩,您可以使用它来手动恢复任何您在激活时更改的环境变量。You were on the right track, but you want to use the
postactivate
hook instead ofpostmkvirtualenv
.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.