Ready to level up your Jupyter Notebook skills? In this tutorial, you’ll not only learn about the power of environment variables, but you’ll also discover the best practices for using them in your notebook. You’ll be a pro at managing your settings and configurations in no time!
Before we dive in, you can skip ahead using the table of contents and follow along using the Jupyter notebook.
What are Environment Variables?
Environment variables are values you pass to a computer’s operating system or an application at runtime. You use these values to configure the operating system’s behavior or applications like Jupyter notebook. Typical use cases are storing settings like file paths, user preferences, and other settings.
printenv command lists all of your environment variables. You’ll learn about this command in a minute. You can easily see the path of my home directory, my name, and that I’m running Ubuntu.
What Is Jupyter Notebook?
Jupyter Notebook is an open-source web application that allows users to create and share documents that contain live code, equations, visualizations, and narrative text.
It’s a powerful tool for data analysis and scientific computing. I use it for data exploration and when prototyping trading strategies.
Environment Variables Python Tutorial
Before we open up Jupyter notebook, let’s learn how to print out the current variables using the terminal, as I demonstrated above.
List Operating System Environment Variables
You can list your existing environment variables using printenv in a Unix-like operating system or the set command in windows.
Now that we’ve covered the basics let’s learn how to work with environment variables in Jupyter Notebook.
There are typically two ways to set an environment variable using Python:
- Set it directly.
- Read from a file.
Setting and Getting Environment Variables
To set an environment variable in Jupyter Notebook, you can use Python to set it using the
os.environ or use magic commands. Let’s cover the os.environ function first.
os.environ function sets environment variables on the operating system where the Jupyter Notebook server is running.
To set the environment variable MY_VAR to the value my_value, enter the following:
You can use the os.getenv function to access environment variables. Let’s grab the MY_VAR variable we set earlier. If you already imported os, you don’t have to do it again.
Now let’s cover magic commands. Magic commands are special commands built into Jupyter Notebook prefixed with
Now that we’ve covered how to set and access environment variables in Jupyter Notebook, let’s discuss dotenv files.
Reading Dotenv Environment Variables
Python-dotenv is a package that enables you to load environment variables directly from a .env file. This is useful for storing sensitive information like database credentials or API keys. It’s also helpful for keeping application configurations such as dev or production environments.
The .env file is a simple text file containing key-value pairs where the keys are the variable names. You can have as many variables as you would like.
To use python-dotenv, install the package using pip. Remember to prepend an exclamation point if running the code in Jupyter notebook.
After installing, use dotenv to load the .env file key-value pairs into your environment.
Notice how we now have MY_OTHER_VALUE, as it wasn’t available to us.
Using Environment Variables for Sensitive Information
One of the most common use cases is to store sensitive information like API keys or database credentials. I use .env files for almost all the code I share for obvious reasons. This allows me to separate sensitive data and ensures I don’t commit it to shared source code.
Top Five Tips Using Environment Variables with Jupyter Notebook
- Use descriptive and unique names for your environment variables to avoid conflicts.
- Remember that environment variables are case-sensitive. Use the XCU specification of all uppercase with underscores if you want to do it right.
- Consider storing sensitive information in environment variables to keep them separate from your code.
- Use the printenv or set command from a terminal or prepending a ! when within Jupyter notebook to list environment variables.
- Use the python-dotenv package to manage environment variables in a .env file.
Frequently Asked Questions
How can I list environment variables in Jupyter Notebook?
Use the built-in %env magic command or pass the printenv or set commands to the operating system.
Why is Jupyter Notebook not seeing my environment variables?
The most common reasons are:
1. They’re not correctly set in your operating system. Check the settings using printenv or set from a terminal to verify.
2. The notebook doesn’t have access to your user-level variables in your bash profile. You may need to set the environment variables at the system-level in
3. The values are not passed correctly to Jupyter Notebook. Use the
%env magic to diagnose.
How do I reload environment variables in Jupyter Notebook?
Reload any modules or environment variables that have changed by using the
%reload_ext autoreload magic command.