How can you set environment variables in a virtualenv in Windows?
Asked Answered
C

1

11

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.

Caucus answered 4/10, 2017 at 13:45 Comment(0)
P
22

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=
Pr answered 6/10, 2017 at 16:16 Comment(1)
Cool. Will this also unset the environment variable when I deactivate the virtualenv?Caucus

© 2022 - 2024 — McMap. All rights reserved.