'Running Django management commands in Visual Studio with .env file environment variables

I need to remove all sensitive keys from the code in my Django app, even those keys that are just used for local testing.

The Visual Studio debugger can load environment variables from a .env file. I followed the official tutorial and it's worked, the dev server runs and my Django app can access environment values that are stored in the .env file.

But I've hit an issue when I run custom Django management comments. Is there a way to run the commands from the debugger so that they can also access the environment variables? Or some other way to run the commands so they can access the key-value pairs in the .env file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source