Question: I'm trying to build a slackbot and retrieve the slack token from a separate .env file. When I run it, I get thrown an error that looks like this:

```
raise KeyError(key) from None
KeyError: 'SLACK_TOKEN'

```

The code for the bot (ShoppingListBot.py) is here:

```
import slack
import os
from pathlib import Path
from dotenv import load_dotenv

env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)

client = slack.WebClient(token=os.environ["SLACK_TOKEN"])

```

The code for the .env file (.env) is here:

```
SLACK_TOKEN="xoxb-1691324762768-1693412284260-RdP0ZQUaQxD9j9mtLlwfNMbD"

```

Answer: 
Instead of `os.environ["SLACK_TOKEN"]` you should use `os.getenv("SLACK_TOKEN")`.

From the [docs](https://docs.python.org/3/library/os.html#os.environ) (emphasis mine):

> 
> This mapping is captured the **first time the os module is imported**, typically during Python startup as part of processing site.py. **Changes** to the environment made after this time **are not reflected** in os.environ, except for changes made by modifying os.environ directly.
> 
> 
> 

Or use `override=True` in `load_dotenv()`
I usally use YAML files but it would appear you should use `os.getenv("SLACK_TOKEN")` not `os.environ["SLACK_TOKEN"]` according to [pip python-dotenv](https://pypi.org/project/python-dotenv/)