Python

Secure secrets injection for Python applications in Visual Studio Code.

This guide will show you how to seamlessly integrate Doppler into your Python application development and debugging workflow inside Visual Studio Code.

Prerequisites

Setup

Ensure you've authenticated the Doppler CLI so secrets can be accessed from your machine:

doppler login

Then configure the Doppler CLI to select which project and environment to fetch secrets from by running the following command from a terminal within Visual Studio Code:

doppler setup

Configuration

Because Visual Studio Code uses a custom Microsoft Python debug library, the Doppler CLI cannot be used to run your application directly.

That's why we created the doppler-env package which when activated with the DOPPLER_ENV environment variable, injects secrets as environment variables into the Python debug process prior to your application code being run.

📘

Ensure Visual Studio Code is configured to use the Python interpreter belonging to this application's virtual environment by running the Python: Select Interpreter command.

Install the doppler-env package in your virtual environment:

pip install doppler-env
pipenv install doppler-env --dev
poetry add doppler-env@latest --dev

Then simply update your Python launch configuration to add the DOPPLER_ENV environment variable:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "python: server",
      "type": "python",
      "python": "${env:HOME}/.virtualenvs/python-web3/bin/python",
      "request": "launch",
      "program": "web3-start.py",
      "env": {
        "DOPPLER_ENV": "1" // Enable doppler-env to inject env vars
      }
    }   
  ]
}

👍

Awesome Work!

Now you know how to inject Doppler secrets into your Python applications when debugging with Visual Studio Code.