This video shows you how to set an API key as an environment variable directly in your shell's `~/.zshrc` configuration file using the `export` command and access it from Python using `os.environ.get`, you can follow these steps:
1. Open your preferred text editor and open the `~/.zshrc` file. For example, you can use the following command to open it with the Nano editor:
```bash
nano ~/.zshrc
```
2. Scroll to the end of the file or the appropriate section where you want to add the environment variable.
3. Add the following line to set the environment variable:
```bash
export API_KEY="your_api_key_here"
```
Replace `"your_api_key_here"` with your actual API key.
4. Save the changes and exit the text editor. In Nano, you can press `Ctrl + X`, then `Y` to save the changes.
5. To apply the changes to your current session, either close and reopen the terminal or run the following command to reload the shell configuration file:
```bash
source ~/.zshrc
```
6. In your Python script, you can access the API key using `os.environ.get` method. Here's an example:
```python
import os
api_key = os.environ.get('API_KEY')
if api_key:
# Use the API key in your code
# ...
else:
# API key not set
# Handle the case accordingly
# ...
```
The `os.environ.get('API_KEY')` method retrieves the value of the `API_KEY` environment variable. It returns `None` if the environment variable is not set, allowing you to handle that case appropriately.
By following these steps, you'll be able to set an API key as an environment variable in your `~/.zshrc` configuration file and access it from your Python script using `os.environ.get`.
Become a patron : ?
https://www.patreon.com/drpiBuy me a coffee (or Tea) ☕
https://www.buymeacoffee.com/DrPiIf you want a fast VPS server with Python installed check out :
https://webdock.io/en?maff=wdaff--170https://webdock.io/en/black-friday-cyber-monday-weekend-2022Pytest with FastAPI course on "TestDriven.io":
-------------------------------------------------------------------------
https://testdriven.io/courses/tdd-fastapi/?utm_source=python360https://testdriven.io/courses/scalable-fastapi-aws/?utm_source=python360https://testdriven.io/courses/fastapi-celery/?utm_source=python360Thumbs up yeah? (cos Algos..)
#hideapikey #openai #python
...
https://www.youtube.com/watch?v=b6pknmBGYNg