pythonsetuptoolspackagingdistutils.env

.env files and python package distribution


I am confused about something. I have a python package that relies on hard coded values in a Config class. Having done some reading, I want to move to a .env file and load the values from the .env file into the Config class. No issues here.

My confusion is around the topic of packaging/distribution. My package will be installed on machines and run locally by the user. Based on my approach above, I am confused on how to ensure the values from the .env file are included in the packaging phase.

I originally used a setup.py file and distutils but I am migrating to pyproject.toml and build for packaging.

I would appreciate any hints on how to to achieve this.

I have searched the setuptools docs and I am not seeing anything obvious. I do see a reference to an env in Entry Points require(env=None, installer=None) but am not sure if this is the same environment I am referring to.

I have also checked out the distutils docs and found distutils.util.check_environ() but again I can't see how to set the environment.

Edit: Just to clarify, if it is perfectly normal to include the .env file in packaging and distribution, then I am good to go. If it is not, then my question still stands I suppose


Solution

  • If your .env file contains secrets, then I would not package it. In my team we are in a similar situation. We build packages for analysts to use in their Notebooks. The packages connect to all kind of data sources, and therefore we use .env files for setting credentials and stuff. We simply wrote documentation for the users how to create their own .env files. That's it. If you want to use the packages in a cloud application, then you can probably pass the environment variables in a build pipeline. We use Pydantic, and with their dotenv support, you can either use .env files, or read from your environment variables directly (https://docs.pydantic.dev/latest/concepts/pydantic_settings/#dotenv-env-support)