In Rails, there are many descriptions of environment variables that you do not want to show to the outside, Since there are few Pythons, the method of describing environment variables in Python is described.
-How to upload to GitHub without showing environment variables (API key, etc.) -You can specify a template, and you can use it immediately by inserting the API key.
Python's dotenv has a slightly different name than python-dotenv. https://github.com/theskumar/python-dotenv
Install
$ pip install python-dotenv
See other sites for how to use pip install
.env
API_KEY=XXXXXXXXX
Enter the environment variables with the file name **. env **. Also, to know the .env template when cloned on GitHub, It's a good idea to have something called **.env.sample **.
c:.env.sample
API_KEY=
Of course, refrain from entering the API key in .env.sample. .env.sample is a template, so push it to GitHub.
settings.py
# coding: UTF-8
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
AP= os.environ.get("API_KEY") #Assign the value of the environment variable to the AP
Since there is a variable AP in which the value of the environment variable is assigned to settings.py, import settings.py.
sample.py
# coding: UTF-8
import settings.py
API_KEY = settings.AP
##Source code thereafter
This article has few samples, so I actually made it I will post the source code that allows you to tweet on Twitter. https://github.com/CrowCrowzard/tweet
Recommended Posts