Skip to main content
  1. Blog posts/

How to hide enviroment variables using .env

·1 min

This article explains how to hide environment variables using .env file.

I will use python-dotenv within Python to hide the .env file.

Installing python-dotenv #

Install python-dotenv using the pip install python-dotenv code.

Organizing various environment variables #

Organize various sensitive environment variables, such as API keys, in the .env file to avoid uploading them to Github.

Activating the .env file #

pythonCopy code
from dotenv import load_dotenv
import os

# Activating the .env file
load_dotenv()

SERVICE_KEY = os.getenv('SERVICE_KEY')

Checking the .gitignore file #

Typically, the .gitignore file already includes .env, but it’s good to double-check before pushing to Github. If the .env file is properly set up, you can proceed with git add -> commit -> push.

If variables are stored in a file other than .env, you need to specify it in the .gitignore so it won’t be uploaded to Github.

Pushing to git #

After pushing to git, check Github.

If you don’t see the .env file uploaded, then it’s been handled correctly.