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!

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.


If you found this helpful you should subscribe to my newsletter for other tips and tricks.