Poetry Dotenv Plugin
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
-
Environment variables not set!
cause The `.env` file is not located in the current working directory or a parent directory, or the plugin is not correctly installed/activated.fixEnsure the `.env` file is in the project root or specify its path using `POETRY_DOTENV_LOCATION` environment variable or `[tool.dotenv].location` in `pyproject.toml`. Verify the plugin is installed globally with `poetry self show plugins`. -
Poetry does not recognize 'poetry-dotenv-plugin' or 'plugin command not found'.
cause The plugin was not correctly installed using `poetry self add` or there's a conflict with your Poetry version.fixReinstall the plugin using `poetry self add poetry-dotenv-plugin`. Ensure your Poetry version meets the plugin's requirements (typically Poetry 1.2+ for plugins). Check `poetry self show plugins` to confirm activation. -
Unexpected environment variable values (e.g., system variables not overridden, or vice versa).
cause Misunderstanding the plugin's default override behavior or incorrect configuration for `override_existing`.fixReview the plugin's default behavior regarding environment variable overriding. If on version 0.2.0+, use `[tool.dotenv].override_existing = false` in `pyproject.toml` or `POETRY_DOTENV_OVERRIDE_EXISTING=false` to prevent overriding existing system variables.
Warnings
- breaking Version 0.2.0 changed how development dependencies are managed in `pyproject.toml`, switching from the deprecated `[tool.poetry.dev-dependencies]` section to Poetry's new `[tool.poetry.group.dev.dependencies]` groups.
- gotcha Poetry plugins are installed globally for your Poetry installation, not on a per-project basis. This means `poetry-dotenv-plugin` will attempt to load `.env` files for *any* Poetry project you work on, which might lead to unexpected variable loading if `.env` files exist in unrelated projects.
- gotcha By default, the plugin will override existing environment variables with those found in the `.env` file. The 0.2.0 release added an option to prevent this, but the default behavior remains to override unless configured otherwise.
Install
-
poetry self add poetry-dotenv-plugin
Imports
- poetry-dotenv-plugin
from poetry_dotenv_plugin import load_dotenv
This is a Poetry plugin and is not imported directly into Python code. Its functionality is activated automatically by Poetry.
Quickstart
# 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