When you do any project-specific development, you always want to avoid setting anything for your whole system. This holds for your third-party Python packages. The same counts for secrets, which are usually project-specific.
It's a much better idea to set these types of environment variables inside of your virtual environment. In this course, these types of variables will be called virtual environment variables.
Using environment variables inside of a Python virtual environment is easier than having to first export and then unset each variable every time that you want to work on a project.
Editing Your Activation Script
Start by creating a virtual environment for your Python project:
python3 -m venv venv
After you successfully create a virtual environment, open the activate script in your favorite text editor.
Info: You can find this file inside of the venv folder that was created by running the command shown above. The relative path of this script is venv/bin/activate on UNIX systems and venv\Scripts\activate on Windows.
This script runs every time your venv gets activated, which makes it a good place to let your computer know which environment variables you would like to have access to and which ones to get rid of once you exit the virtual environment.
You'll now edit the activate script and hard-code the values you want in there. First, you need to make sure that your virtual environment variables won’t stick around once you deactivate them, so you start by unsetting the environment variable that you haven't even created yet.
Unsetting Virtual Environment Variables
To unset a virtual environment variable, you add an unset command to the deactivate command section in your Bash activate script. The code in this part of the script runs every time you deactivate your virtual environment.
For example, to unset the variable MY_SUPER_SECRET_SECRET, you need to add the following line of Bash code:
deactivate () {
unset MY_SUPER_SECRET_SECRET
# Lots of other code
}
Adding this line in the deactivate section makes sure that your virtual environment variables won't leak into your system environment. Instead, they'll exist only within your virtual environment.
Once you write the code to unset your variable, it's time to make sure you also set it so it'll exist in the first place.
Setting Virtual Environment Variables
You can set a virtual environment variable in the same way as you practiced before with your Bash CLI. However, instead of typing the export command directly in your terminal, you'll add it as a new line of code at the end of the activate script:
# The rest of the script
export MY_SUPER_SECRET_SECRET="OMG this is so secret I can't even say!"
Info: You'll need to wrap the value of your virtual environment variable inside of double quotes ("").
Save the Bash script and close it. Now you can activate your virtual environment:
source venv/bin/activate
Once the virtual environment has been successfully activated, you can now run the printenv command to inspect the state of your environment variables in your current environment.
MY_SUPER_SECRET_SECRET should show up, as should the value you assigned to it.
After you confirm that activating your virtual environment brings your virtual environment variable into existence, go ahead and deactivate it. Use printenv once again. Your secret should be gone.
As you can see, this setup can keep your project-specific secrets safe within their own comfy virtual environments.
Accessing Virtual Environment Variables
To use one of your virtual environment variables inside of your Python project, you need to access it. You can do that, for example, with Python's os module that comes packaged with the standard library:
import os
secret = os.environ['MY_SUPER_SECRET_SECRET']
print(secret)
You'll be able to run this code from any file in your project as long as your virtual environment is activated and an environment variable called MY_SUPER_SECRET_SECRET is defined.
If you don't want your script to terminate with an exception when you didn't define the environment variable, then you can use Python's dict.get() method instead of doing a direct lookup.
In a future section, you'll dive a bit deeper into more advanced Python concepts as well as learn about APIs and databases. This will prepare you to use Python to interact with the world-wide web.
Summary: Python, Create Venv Environment Variables
- Setting environment variables inside of your Python virtual environments allows you to easily access project-specific setting information while reducing the danger of accidentally committing them to public version control.
- Make sure that you add your virtual environment folder to your
.gitignorefile, or you'll end up pushing your secrets to GitHub after all. - Don't worry too much about GitHub and version control if you're not yet familiar with it. You can head over to the Git and GitHub course to study up any time you feel like it. For now, it's not essential to understand it in order to continue with this course.
- In these lessons about environment variables, you learned how to:
- Add and remove Python environment variables in Bash
- Set up project-specific environment variables inside of your Python virtual environments
- Access environment variables with Python