pythonincludepython-packagingtoml.env

How to include .env file in my python package using pyproject.toml?


I have the following folder structure:

program_root/
    - src/
        - tests/  
    - data/
        - templates/
    - docs/
    - build/                            
    - dist/             
    - pyproject.toml        
    - setup.py          
    - README
    - requirements.txt

My src/ layout:

src/
   program/
    - _init_.py
    p1/
      - .env                
      - module2.py
      - module3.py
    p2/
      -module4.py
      -module5.py

I've packaged this and installed it into my venv itself to see if it's working fine. I found that my .env file isn't being included in the p1 package and rest all of the files are being included.

(I have no secrets stored in .env file and happy to include it in my package)

I have included the following in my toml file:

[tool.setuptools.package-data]
"*" = ["*.*"]

As glob pattern indicates, all files in my src folder should be included. but that isn't the case, all .py files are being included but .env file isn't (I do not have any other file types except ``.env`` and ``.py`` in my src folder).
My toml file (for reference):

[build-system]
requires = ["setuptools>=67.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
package-dir = {"" = "src"}

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"*" = ["*.*"]

[project]
name = "program"
version = "1.0.0"
authors = [
  {name="xyz", email="xyz@abc.com" },
]
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

Please help me understand if I am doing anything wrong here.

Using

[tool.setuptools.package-data]
'src' = ['.env']

from How to include .json files in a python package using a pyproject.toml?

didn't work.

My below answer did help me. But, I am confused as to why the following is not enough for setuptools to include all files in my package.

[tool.setuptools.package-data]
"*" = ["*.*"]

Solution

  • In the MANIFEST.in file (at the same level as src) I have included the following: recursive-include src/program * It solved the issue.