Poetry Dotenv Plugin

0.2.0 · active · verified Thu Apr 16

A Poetry plugin that automatically loads environment variables from `.env` files into the environment before Poetry commands are run. It is currently at version 0.2.0 and provides an essential feature for managing project-specific configurations by integrating seamlessly with Poetry's workflow.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install the `poetry-dotenv-plugin`, define environment variables in a `.env` file, and then access them within a Python script executed using `poetry run`. The plugin automatically loads the variables without explicit code changes.

# 1. Install the plugin (if not already installed)
# poetry self add poetry-dotenv-plugin

# 2. Create a .env file in your project root
# echo 'MY_SECRET_KEY="supersecret"' > .env
# echo 'DATABASE_URL="postgresql://user:password@host:port/dbname"' >> .env

# 3. Create a Python script (e.g., main.py)
# print('import os\n\nif __name__ == "__main__":\n    print(f"My secret key: {os.environ.get(\'MY_SECRET_KEY\', \'NOT_SET\')}")\n    print(f"Database URL: {os.environ.get(\'DATABASE_URL\', \'NOT_SET\')}")') > main.py

# 4. Run the Python script via Poetry
poetry run python main.py

view raw JSON →