diff options
author | 2021-05-10 16:57:02 +0200 | |
---|---|---|
committer | 2021-05-10 16:57:02 +0200 | |
commit | ac5a4953605ba463d322dd3f50925bf25052fa85 (patch) | |
tree | d8e6f390c01ef72105755da323c9556ec1d98d6a | |
parent | Merge pull request #1542 from RohanJnr/annihilate_reddit (diff) | |
parent | chore: add pypi link for python-dotenv (diff) |
Merge pull request #1579 from python-discord/vcokltfre/tag/dotenv
feat: add dotenv tag
-rw-r--r-- | bot/resources/tags/dotenv.md | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/bot/resources/tags/dotenv.md b/bot/resources/tags/dotenv.md new file mode 100644 index 000000000..acb9a216e --- /dev/null +++ b/bot/resources/tags/dotenv.md @@ -0,0 +1,23 @@ +**Using .env files in Python** + +`.env` (dotenv) files are a type of file commonly used for storing application secrets and variables, for example API tokens and URLs, although they may also be used for storing other configurable values. While they are commonly used for storing secrets, at a high level their purpose is to load environment variables into a program. + +Dotenv files are especially suited for storing secrets as they are a key-value store in a file, which can be easily loaded in most programming languages and ignored by version control systems like Git with a single entry in a `.gitignore` file. + +In python you can use dotenv files with the [`python-dotenv`](https://pypi.org/project/python-dotenv) module from PyPI, which can be installed with `pip install python-dotenv`. To use dotenv files you'll first need a file called `.env`, with content such as the following: +``` +TOKEN=a00418c85bff087b49f23923efe40aa5 +``` +Next, in your main Python file, you need to load the environment variables from the dotenv file you just created: +```py +from dotenv import load_dotenv() + +load_dotenv(".env") +``` +The variables from the file have now been loaded into your programs environment, and you can access them using `os.getenv()` anywhere in your program, like this: +```py +from os import getenv + +my_token = getenv("TOKEN") +``` +For further reading about tokens and secrets, please read [this explanation](https://vcokltfre.dev/tips/tokens). |