Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Windows)
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Hardcoding database passwords or API keys in Python source code increases the risk of credential leaks when code is shared or committed to repositories.
Briefing
Hardcoding passwords and secret API keys directly in Python code is a common beginner mistake—and it becomes dangerous fast when code is shared with a team or pushed to a repository. A safer pattern is to store secrets in environment variables on the machine running the code, so the source code can be shared without exposing credentials.
On Windows, environment variables are set through the Control Panel. The process starts by opening the Windows search, launching Control Panel, then going to System and Security → System. From there, Advanced system settings leads to the Environment Variables button. In that dialog, secrets can be added as either user variables or system variables; the walkthrough uses user variables. New variables are created with names like DB_USER and DB_PASS (the tutorial prefers all-caps for clarity), and their values are copied from the placeholders that previously lived inside the Python script.
After saving the variables, the change may not apply immediately to an already-open development environment. The tutorial specifically notes that restarting the IDE or editor (it uses Sublime Text) ensures the updated environment is visible to the running Python process.
In Python, the secrets are retrieved at runtime using the standard library’s OS module. Importing OS enables access to environment variables via os.environ, which behaves like a dictionary. Each secret is fetched by key—e.g., os.environ['DB_USER'] for the database username and os.environ['DB_PASS'] for the database password. When the script runs, it prints or uses the retrieved values without those credentials being embedded in the code itself.
The practical payoff is straightforward: sharing the script no longer shares the secrets. Anyone who receives the code sees only the environment-variable lookups, while the actual credentials remain stored locally on the machine where the variables were defined. The tutorial also points out that setting environment variables differs across operating systems, and it references a separate walkthrough for Mac and Linux.
Overall, the method is a quick, low-friction way to reduce accidental credential leaks during collaboration and version control—especially for developers who are still learning good security hygiene. It doesn’t replace more advanced secret-management systems, but it addresses the immediate risk of committing or distributing secrets alongside application code.
Cornell Notes
Storing credentials inside Python source code makes secrets easy to leak when code is shared or pushed to a repository. On Windows, environment variables provide a simple alternative: define variables like DB_USER and DB_PASS in the system’s Environment Variables settings, then restart the IDE so the running process can see them. In Python, import the OS module and read values from os.environ like os.environ['DB_USER'] and os.environ['DB_PASS']. This keeps the code shareable while the actual secrets remain on the local machine. The approach is presented as a beginner-friendly security improvement for database logins, API keys, and other sensitive values.
Why is hardcoding secrets in a Python script risky when working with teams or repositories?
How do you set environment variables on Windows for use by a Python script?
What change might be required after setting environment variables, and why?
How does Python retrieve environment-variable secrets at runtime?
What’s the main security benefit of this approach compared with storing secrets in code?
Review Questions
- What are two concrete reasons secrets become exposed when they are hardcoded in source code?
- Walk through the Windows steps to create environment variables and name the variables used in the example.
- In Python, what does os.environ represent, and how do you access a specific secret from it?
Key Points
- 1
Hardcoding database passwords or API keys in Python source code increases the risk of credential leaks when code is shared or committed to repositories.
- 2
On Windows, environment variables are configured via Control Panel → System and Security → System → Advanced system settings → Environment Variables.
- 3
Create user variables for secrets (e.g., DB_USER and DB_PASS) and store the real credential values as the variable values.
- 4
Restart the IDE/editor after adding environment variables so the running Python process can read the updated environment.
- 5
In Python, import os and read secrets from os.environ using dictionary-style key access like os.environ['DB_USER'].
- 6
Sharing the script becomes safer because the code contains only environment-variable lookups, not the actual secrets.