Hide your API key | Python os.environ.get

Published: 30 June 2023
on channel: jobstr
1,319
6

This video shows you how to avoid accidentally putting your OpenAI API key on GitHub

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 : 🌏   / drpi  
Buy me a coffee (or Tea) ☕ https://www.buymeacoffee.com/DrPi

If you want a fast VPS server with Python installed check out :
https://webdock.io/en?maff=wdaff--170

Pytest with FastAPI course on "TestDriven.io":
-------------------------------------------------------------------------
https://testdriven.io/courses/tdd-fas...
https://testdriven.io/courses/scalabl...
https://testdriven.io/courses/fastapi...


Thumbs up yeah? (cos Algos..)

#hideapikey #openai #python


On this page of the site you can watch the video online Hide your API key | Python os.environ.get with a duration of hours minute second in good quality, which was uploaded by the user jobstr 30 June 2023, share the link with friends and acquaintances, this video has already been watched 1,319 times on youtube and it was liked by 6 viewers. Enjoy your viewing!