python-openv


Namepython-openv JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/harttraveller/python-openv
SummaryBasically the same as python-dotenv except integrated with 1password.
upload_time2024-01-07 23:03:56
maintainer
docs_urlNone
authorHart Traveller
requires_python>3.9.7,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # python-openv

Installation:

```sh
pip install python-openv
```

Usage:

```python
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](https://github.com/theskumar/python-dotenv). It uses the [1password-client](https://github.com/wandera/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

1. In order to use this, you will need to install the [1Password CLI](https://developer.1password.com/docs/cli/get-started/) and turn on the desktop app integration.
2. You need to have a 1password vault named `.env`.
3. 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:

```sh
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).


```python
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:

```sh
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.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/harttraveller/python-openv",
    "name": "python-openv",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">3.9.7,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Hart Traveller",
    "author_email": "ruling_fiction.0b@icloud.com",
    "download_url": "https://files.pythonhosted.org/packages/f1/42/0937eae8f195b3f3e9431d01d4fd7e24650903e3fadde9eaac9bdbf09c37/python_openv-0.1.1.tar.gz",
    "platform": null,
    "description": "# python-openv\n\nInstallation:\n\n```sh\npip install python-openv\n```\n\nUsage:\n\n```python\nfrom openv import load_openv\n\nload_openv(project=\"my_project_name\")\n```\n\n## Overview\n\nThis is a simple utility for loading project environment variables from 1password. It works more or less the same as [python-dotenv](https://github.com/theskumar/python-dotenv). It uses the [1password-client](https://github.com/wandera/1password-client) python package to access fields.\n\n## Purpose\n\nIn 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.\n\n## Requirements\n\n1. In order to use this, you will need to install the [1Password CLI](https://developer.1password.com/docs/cli/get-started/) and turn on the desktop app integration.\n2. You need to have a 1password vault named `.env`.\n3. The title of each item in that vault must be unique within the vault.\n\n## Example Workflow\n\nSuppose 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.\n\nIf we're using poetry for the app, we might add `python-openv` to the dev dependencies with:\n\n```sh\npoetry add python-openv --group dev\n```\n\nAfter creating a `.env` vault in 1password, you might add an item called `dad_joke_generator`, and add the following password field: `OPENAI_API_KEY`.\n\nThen, 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).\n\n\n```python\nimport os\nimport uvicorn\nfrom openai import OpenAI\nfrom fastapi import FastAPI\n\ntry:\n    from openv import load_openv\n    load_openv(\"dad_joke_generator\")\nexcept:\n    pass\n\n# The name of the field in the .env 1password vault.\nOPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n\nclient = OpenAI(api_key=OPENAI_API_KEY)\n\napp = FastAPI()\n\n@app.get(\"/joke\")\nasync def joke():\n    chat_completion = client.chat.completions.create(\n        messages=[\n            {\n                \"role\": \"user\",\n                \"content\": \"Tell me a dad joke.\",\n            }\n        ],\n        model=\"gpt-3.5-turbo\",\n    )\n    return chat_completion\n\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=HOST, port=PORT)\n\n```\n\nNote 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.\n\nAlso, in this case, when you deploy the app you would run:\n\n```sh\npoetry install --without dev\n```\n\nIn 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.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Basically the same as python-dotenv except integrated with 1password.",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://github.com/harttraveller/python-openv/README.md",
        "Homepage": "https://github.com/harttraveller/python-openv",
        "Repository": "https://github.com/harttraveller/python-openv"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "76fbf0e423f614bec3894225b4d7d8cf93b9334f9bfa6a73f5c3ddaba8cd18e1",
                "md5": "159a7e1dcbac90969fbca0e06f63c44d",
                "sha256": "f423ef184d9fac132a43ed329c593b22a70be903c91e36dd10fad3a1d1fbefc3"
            },
            "downloads": -1,
            "filename": "python_openv-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "159a7e1dcbac90969fbca0e06f63c44d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">3.9.7,<4.0",
            "size": 4828,
            "upload_time": "2024-01-07T23:03:54",
            "upload_time_iso_8601": "2024-01-07T23:03:54.587086Z",
            "url": "https://files.pythonhosted.org/packages/76/fb/f0e423f614bec3894225b4d7d8cf93b9334f9bfa6a73f5c3ddaba8cd18e1/python_openv-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f1420937eae8f195b3f3e9431d01d4fd7e24650903e3fadde9eaac9bdbf09c37",
                "md5": "6b85b9e1e127a97d64a0d556099ba2f9",
                "sha256": "c2f837ca1f0d0674c26566378040c8cb85c2638d97b60ad3c064c42f36fe39af"
            },
            "downloads": -1,
            "filename": "python_openv-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6b85b9e1e127a97d64a0d556099ba2f9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">3.9.7,<4.0",
            "size": 3932,
            "upload_time": "2024-01-07T23:03:56",
            "upload_time_iso_8601": "2024-01-07T23:03:56.081027Z",
            "url": "https://files.pythonhosted.org/packages/f1/42/0937eae8f195b3f3e9431d01d4fd7e24650903e3fadde9eaac9bdbf09c37/python_openv-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-07 23:03:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "harttraveller",
    "github_project": "python-openv",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "python-openv"
}
        
Elapsed time: 3.72628s