How can you set environment variables in a virtualenv in Windows?

Vik78 picture Vik78 · Oct 4, 2017 · Viewed 8k times · Source

I'm working on a Django project and want to set the DJANGO_SETTINGS_MODULE environment variable in my virtualenv. All the solutions I can find on the internet use UNIX shell commands, and I'm running Windows 10. How can I set the environment variable? I'm using virtualenvwrapper if that makes a difference.

Answer

Kyle picture Kyle · Oct 6, 2017

Inside of your virtual environment locate the Scripts\activate.bat script. There you can add some commands to initialize shell variables with the set command. For your project, you may want something like

set DJANGO_SETTINGS_MODULE=settings.local

Once you deactivate the virtual environment, the environment variable will persist and may conflict with that of another environment. Thus, it is good practice to unset environment variables in virtual environments. You can do this by adding the following to the Scripts\deactivate.bat script.

set DJANGO_SETTINGS_MODULE=