You know, I learned this and completely forgot after months of not dealing with it. Google searches are so difficult to reach the right subject.
I’m certain this will be something I refer to in the future..
Setting environment variables via Python is as simple as:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
Of course, this doesn’t permanently set the variable, but if you have a script that needs that envvar, perfect.
Enjoy.
Thank you! Exactly what is was looking for!
Hola! Glad it helped! This is odd, I was just looking at that post last night.. It’s a conspiracy
Haha yeah, perhaps 🙂 Or maybe it’s just very useful! (At least for beginners in Python like myself..)
Hehe, I’m glad. I started writing this stuff down because I noticed I’d forget some problems and research the same problem over and over
Thanks for this — not sure you will respond as this note is 6 years after this post. My question: where do you put the import os… script? What file? Where? Thanks!
Hey Tony, put it wherever it’s most useful to you. Often times that’s somewhere like wsgi.py which is called before any of the django machinery is invoked.. thereby setting the envvar across the whole project.
But really it depends on your use case. Most people I think would be using this to manually set `DJANGO_SETTINGS_MODULE` wherever needed. Say a one off script importing your django models and doing a periodic task.
Thank you