Thursday, July 16, 2020

How to use secrets in Jupyter Notebooks with python-dotenv

It is not a good idea to embed the the secrets (credentials, keys, etc..) that you need in your Python code directly in your Jupyter Notebooks. You can store these secrets in environment variables but an alternative is using the python-dotenv library. Python-dotenv is a library that reads key-value pairs from  a .env file and adds them to environment variables.

To get started you need to install the latest version of python-dotevn and then you will need to create a .env file in the same folder as your .ipynb files.



Tip: You may not be able to create .env file directly in Windows Explorer - use the command prompt and the echo command to create this file.




You can read a key-value pair in just one line of code after you loaded the extension and the os module which provides functions to interact with the operating system.



Source code is available on https://github.com/jorisp/tradingnotebooks/blob/master/dotenv.ipynb

References:

No comments:

Post a Comment