pip – Python setup.py: How to get find_packages() to identify packages in subdirectories

pip – Python setup.py: How to get find_packages() to identify packages in subdirectories

python package

This is like using the src-layout for the foo and bar packages, but the flat layout for the baz layout. Its possible, but requires some custom configuration in the setup.py.

Setuptools find_packages supports a where keyword (docs), you can use that.

setup(
    ...
    packages=(
        find_packages() +
        find_packages(where=./bar-pack) +
        find_packages(where=./foo-pack)
    ),
    ...
)

Since find_packages returns a plain old list, you could also just list your packages manually, and thats arguably easier / less magical.

setup(
    ...
    packages=[baz, bar, foo],
    ...
)

The non-standard directory structure means youll also want to specify the package_dir structure for distutils, which describes where to put the installed package(s).

Piecing it all together:

setup(
    name=mypackage,
    version=0.1,
    packages=[baz, bar, foo],
    package_dir={
        : .,
        bar: ./bar-pack/bar,
        foo: ./foo-pack/foo,
    },
)

The above installer will create this directory structure in site-packages:

.venv/lib/python3.9/site-packages
├── bar
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-39.pyc
├── baz
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-39.pyc
├── foo
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-39.pyc
└── mypackage-0.1.dist-info
    ├── INSTALLER
    ├── METADATA
    ├── RECORD
    ├── REQUESTED
    ├── WHEEL
    ├── direct_url.json
    └── top_level.txt

pip – Python setup.py: How to get find_packages() to identify packages in subdirectories

Leave a Reply

Your email address will not be published.