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.
Metadata
Release files for python-openv 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| python_openv-0.1.1.tar.gz | 3.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_openv-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.8 kB
Release files / python_openv-0.1.1.tar.gz
| Download URL | python_openv-0.1.1.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2f837ca1f0d0674c26566378040c8cb85c2638d97b60ad3c064c42f36fe39af
|
|
BLAKE2b-256 checksum How to use checksums |
f1420937eae8f195b3f3e9431d01d4fd7e24650903e3fadde9eaac9bdbf09c37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0
|
Release files / python_openv-0.1.1-py3-none-any.whl
| Download URL | python_openv-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f423ef184d9fac132a43ed329c593b22a70be903c91e36dd10fad3a1d1fbefc3
|
|
BLAKE2b-256 checksum How to use checksums |
76fbf0e423f614bec3894225b4d7d8cf93b9334f9bfa6a73f5c3ddaba8cd18e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0
|
Security for python-openv 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.