~ 1 min read

How to use .env files with Poetry

I love Poetry, but one thing I always find frustrating is having to add an additional package to my environments to read from .env files and access them as environment variables. I quite often default to Pipenv for simple projects to get round this. Turns out there’s now another way.

Poetry 1.2 will add plugin support to tweak or add to it’s default package management behaviour and @mpeteuil made one for .env files!

Poetry 1.1

You can install 1.2.0a2 and get it the plugin right now like this:

poetry self update --preview
poetry plugin add poetry-dotenv-plugin

Then you should find anything you’ve added to your .env file is accessible to use in your project as an environment variable.

Poetry 1.2+

If you’re using Poetry 1.2+ the plugin command is now slightly different:

poetry self add poetry-dotenv-plugin

Updated 29th September 2022 to include differences on final 1.2 release.

Subscribe for Exclusives

My monthly newsletter shares exclusive articles you won't find elsewhere, tools and code. No spam, unsubscribe any time.