程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to hide API keys using Python

編輯:Python

Hello , I'm yuechuang .

Blog launch :https://bornforthis.cn/posts/19.html

Sometimes you need to store sensitive information in your code , For example, password or API secret key , And in the Python The simplest way to do this is to use os and dotenv modular .

So , You need to install dotenv modular , and os Module is standard Python modular , No installation required .

On the command line , Input :

pip install python-dotenv

Or for Python 3

pip3 install python-dotenv

hypothesis .env The contents of the file are :

API_KEY="abcd123"

A good practice is to include a in your project .env file , It contains examples of each secret variable you are using . The contents of this document are as follows :

API_KEY="abcd123"

To load it into your python In file , You should :

import os
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("API_KEY")

Good! Now? , You are hiding data correctly !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved