Basically the same as python-dotenv except integrated with 1password.
Project description
python-openv
Installation:
pip install python-openv
Usage:
from openv import load_openv
load_openv(project="my_project_name")
Overview
This is a simple utility for loading project environment variables from 1password. It works more or less the same as python-dotenv. It uses the 1password-client python package to access fields.
Purpose
In theory, it's more secure, as someone with access to your computer can't go through your .env
files, and you also don't risk accidentally including the .env
file in version control.
Requirements
- In order to use this, you will need to install the 1Password CLI and turn on the desktop app integration.
- You need to have a 1password vault named
.env
. - The title of each item in that vault must be unique within the vault.
Example Workflow
Suppose you're developing a FastAPI app called dad_joke_generator
that uses the OpenAI API to generate dad jokes. Assuming that you've added the API key to environment variables on whatever service we're deploying on (either directly, or through a service like Doppler or AWS Secrets Manager) - the question is how you reference the API key in your local development environment.
If we're using poetry for the app, we might add python-openv
to the dev dependencies with:
poetry add python-openv --group dev
After creating a .env
vault in 1password, you might add an item called dad_joke_generator
, and add the following password field: OPENAI_API_KEY
.
Then, your FastAPI app might look as follows (I didn't test this app so it might not work, it's just meant to illustrate usage).
import os
import uvicorn
from openai import OpenAI
from fastapi import FastAPI
try:
from openv import load_openv
load_openv("dad_joke_generator")
except:
pass
# The name of the field in the .env 1password vault.
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
app = FastAPI()
@app.get("/joke")
async def joke():
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Tell me a dad joke.",
}
],
model="gpt-3.5-turbo",
)
return chat_completion
if __name__ == "__main__":
uvicorn.run(app, host=HOST, port=PORT)
Note that you probably don't want to implement a production app like this, this is just to demonstrate idea. Of course, you can also use this to dynamically determine the host, port, whether the app reloads, uses debug mode, etc.
Also, in this case, when you deploy the app you would run:
poetry install --without dev
In whatever build script/configuration file you're using. This way python-openv
won't be installed in your production environment, so the import will fail and the value you set in your production environment for that environment variable will be used instead.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_openv-0.1.1.tar.gz
.
File metadata
- Download URL: python_openv-0.1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f837ca1f0d0674c26566378040c8cb85c2638d97b60ad3c064c42f36fe39af |
|
MD5 | 6b85b9e1e127a97d64a0d556099ba2f9 |
|
BLAKE2b-256 | f1420937eae8f195b3f3e9431d01d4fd7e24650903e3fadde9eaac9bdbf09c37 |
File details
Details for the file python_openv-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: python_openv-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f423ef184d9fac132a43ed329c593b22a70be903c91e36dd10fad3a1d1fbefc3 |
|
MD5 | 159a7e1dcbac90969fbca0e06f63c44d |
|
BLAKE2b-256 | 76fbf0e423f614bec3894225b4d7d8cf93b9334f9bfa6a73f5c3ddaba8cd18e1 |