我正在构建一个使用 API token 和 key 来访问服务的系统,但存储它们的最佳位置在哪里?我想将代码推送到 GitHub,而不推送 token 。
目前,我已将它们放入名为 Constants.py
的空白文件中,并在主 Python 文件中导入 Constants.py
。
API_KEY_SERVICE = "ABC123ABC"
主.py:
import Constants
service_key = Constants.API_KEY_SENDGRID
最佳答案
您正在尝试的是从代码中分离敏感信息的正确方法。您应该在 .gitignore
文件中包含 constants.py
,这将阻止 git 跟踪该文件,从而不会将其推送到 github。
对于.gitignore,请引用:https://git-scm.com/docs/gitignore
关于python - Python 最佳实践 - 在哪里存储 API key / token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995350/