How to write environment variables that you don't want to put on [GitHub] Python

Overview

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.

What is dotenv

-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.

Dotenv in python

Python's dotenv has a slightly different name than python-dotenv. https://github.com/theskumar/python-dotenv

Example of actual usage

Install

$ pip install python-dotenv

See other sites for how to use pip install

How to enter environment variables

.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.

Read API key from .env using dotenv

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

Use environment variables

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

Afterword

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

References

python-dotenv

Recommended Posts

How to write environment variables that you don't want to put on [GitHub] Python
Don't write Python if you want to speed it up with Python
How to access environment variables in Python
How to build an environment when you want to use python2.7 after installing Anaconda3
How to build a Django (python) environment on docker
[Python] How to write a docstring that conforms to PEP8
How to build a Python environment on amazon linux 2
How to build a new python virtual environment on Ubuntu
Don't lose to Ruby! How to run Python (Django) on Heroku
How to write a metaclass that supports both python2 and python3
When you want to hit a UNIX command on Python
How to write a Python class
Notes on how to write requirements.txt
How to rebuild python environment from pyenv on Mac environment (El Capitan)
How to write code to access python dashDB on Bluemix or local
Don't you want to say that you made a face recognition program?
I want to know if you install Python on Mac ・ Iroha
How to use Serverless Framework & Python environment variables and manage stages
How to use python put in pyenv on macOS with PyCall
[Python] When you want to use all variables in another file
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to read pydoc on python interpreter
How to dynamically define variables in Python
[Kivy] How to install Kivy on Windows [Python]
How to write Python document comments (Docstrings)
[Itertools.permutations] How to put permutations in Python
A memo that allows you to change Pineapple's Python environment with pyenv
I want to build a Python environment
Steps to install Python environment on Ubuntu
How to prepare Python development environment [Mac]
How to erase Python 2.x on Mac.
Memorandum on how to use gremlin python
To reference environment variables in Python in Blender
How to add python module to anaconda environment
How to read environment variables from .env file in PyCharm (on Mac)
How to write Ruby to_s in Python
python I don't know how to get the printer name that I usually use.
I want to use Python in the environment of pyenv + pipenv on Windows 10
How to build an environment for using multiple versions of Python on Mac
Visualize the timeline of the number of issues on GitHub assigned to you in Python
How to run GUI programs such as tkinter in Python environment on WSL2
How to write files that you should be careful about in all languages
How to put python library numpy, scipy, matplotlib, ipython, PIL, basemap on Mac
How to write urlfetch unittest on GAE / P
Put MicroPython on Windows to run ESP32 on Python
How to create a Python virtual environment (venv)
[2020 version] How to install Python3 on AWS EC2
Strategy on how to monetize with Python Java
How to install OpenCV on Jetson Nano Python
[Latest] How to build Java environment on Ubuntu
How to use Python Kivy ④ ~ Execution on Android ~
How to run MeCab on Ubuntu 18.04 LTS Python
To write to Error Repoting in Python on GAE
[Python] How to sort instances by instance variables
[Python] How to install OpenCV on Anaconda [Windows]
How to build Java environment on Ubuntu (Linux)
I want to write to a file with Python
Use os.getenv to get environment variables in Python
Do you want me to fix that copy?
How to pass args or environment variables with Makefile and make command on #Linux
[Python] How to write type annotations for Callable objects treated as variables and arguments