3 ways to access credentials in Jupyter Notebook
Have you ever wondered how to keep your credentials in Jupyter Notebook? I guess so! We will show you three ways to handle this problem and a tip about sharing them with other users without showing them your sensitive data.
1. Export environment variable using terminal
You can define your variables using the terminal before opening your notebook. This simple command looks like this:
export DB_NAME=variables_value
Then you have to open your notebook just like you always do by typing that phase in the terminal:
jupyter lab
Next, write this code so you can check if it's working:
Code explanation:
First, you must import os library (Miscellaneous operating system interfaces). Then, you use os.environ
to access your credentials, and if you declared them correctly earlier, your variable value should appear.
Code to copy:
import os
os.environ["DB_NAME"]
Because it is an environment variable, you can check if it still exists after you close your notebook. You can do it using this command:
env | grep DB_NAME
And, of course, it exists, but only in this terminal session. When you close the current terminal, all of the environment variables you created will disappear.
2. Export environment variable during the opening Jupyter Lab
You can also define your variables during the Jupyter Lab process. All you have to do is type this command into your terminal:
DB_NAME=variables_value jupyter lab
You can check if you do everything correctly using the same code that we used earlier.
Credentials declared this way will be only usable during the current process of your Notebook. If you close your Notebook, you won't be able to check the value of it using the terminal because this variable won't exist.
Funfact
When you define your variable before opening Jupyter using this command:
export DB_NAME=variables_value
Then you define a variable with the same name while opening Jupyter, typing this into the terminal:
DB_NAME=other_value jupyter lab
Which value will appear when you check this, just like we did earlier? It's a kinda tricky question, but the answer is...
Conclusion? Variables defined during opening Jupyter overwrite variables declared earlier using the export
command.
3. Export environment variable using .env file
First of all, you have to create a .env file. You can do it using your terminal:
pico .env
Using this command, you will create the file and write something in it at once. We created 2 example variables: DB_USERNAME (test_user) and DB_PASSWORD (supersecret). Now, you have to open your Notebook and write the following code:
Code explanation:
We imported the load_dotenv
function from the dotenv library and the os
library like earlier. In the next step, we used load_dotenv()
to get variables from the .env file. Then we use os.environ
to check if everything is all right.
Code to copy:
import os
from dotenv import load_dotenv
load_dotenv()
os.environ["DB_USERNAME"]
os.environ["DB_PASSWORD"]
We can also optimize this code:
Code explanation:
As you can see, we added a few things. We used os.environ.get
instead of os.environ
because without get
if the variable didn't read correctly, it would cause an error. While using get
in this situation, the variable's value is set to None
or another value you can specify. The next step is creating if
to check if our variables read correctly.
Code to copy:
import os
from dotenv import load_dotenv
load_dotenv()
name = os.environ.get("DB_USERNAME")
password = os.environ.get("DB_PASSWORD")
if the name is not None and password is not None:
print("credentials loaded successfully")
else:
print("credentials didn't load successfully")
4. Sharing Notebooks with other users
When sharing Notebooks, concerns about revealing private files and secrets can be a major issue. However, with Mercury, you can share your notebooks confidently, knowing that sensitive information remains secure. Our step-by-step tutorial demonstrated just how easy it is to ensure privacy while collaborating.
Check this out right here: Secrets Management.