Python Adding Credentials to Scripts

This is a handy Python library to use when credentials are required to be used by scripts. Credentials shouldn’t ever be inside of scripts.
Python-dotenv loads the environment variables from a .env file. The OS module is needed to use these environment variables.

Install

Create .env File

Use .env in Script

I have used this in my Netmiko Tacacs Project Script. This is a small example section. The full details can be found in the project details or GitHub.

Avoid Adding to Git with .gitignore

Now that the credentials are out of the script, we don’t want to add these to our Git. A .gitignore file is used so Git will not include these files. All we need to do is create the .gitignore file and add the .env file name in there.

Do Include a .env.example with example credentials in

Leave a Comment

Your email address will not be published. Required fields are marked *