Search code examples
pythonsetuptoolsdistutils

Setuptools how to package just some modules and files


I'm packaging a little python package. I'm a complete newbie to python packaging, my directory structure is as follows (up to second level nesting):

.
├── data
│   ├── images
│   ├── patches
│   └── train.csv
├── docker
│   ├── check_gpu.py
│   ├── Dockerfile.gcloud_base
│   ├── Dockerfile.gcloud_myproject
├── env.sh
├── gcloud_config_p100.yml
├── legacy
│   ├── __init__.py
│   ├── notebooks
│   └── mypackage
├── notebooks
│   ├── EDA.ipynb
│   ├── Inspect_patches.ipynb
├── README.md
├── requirements.txt
├── scripts
│   ├── create_patches_folds.py
│   └── create_patches.py
├── setup.py
├── mypackage
    ├── data
    ├── img
    ├── __init__.py
    ├── jupyter
    ├── keras_utils
    ├── models
    ├── train.py
    └── util.py

My setup.py:

import os
from setuptools import setup, find_packages

REQUIRED_PACKAGES = [
    "h5py==2.9.0",
    "numpy==1.16.4",
    "opencv-python==4.1.0.25",
    "pandas==0.24.2",
    "keras==2.2.4",
    "albumentations==0.3.1"
]

setup(
    name='mypackage',
    version='0.1',
    install_requires=REQUIRED_PACKAGES,
    packages=find_packages(include=["mypackage.*"]),
    include_package_data=False
)

The code i want to package corresponds only to the mypackage directory. That's why i passed "mypackage.*" to find_packages and used include_package_data=False.

If i run:

python setup.py sdist

All project structure gets packaged in the resulting tar.gz file.

Anyone knowing how to just package modules inside mypackage/ and the top level README file? I'm not finding this in setuptools docs.


Solution

  • First thing to fix is

    packages=find_packages(include=["mypackage"]),
    

    But you also need to understand that sdist is mostly controlled by the files MANIFEST or its template MANIFEST.in, not setup.py. You can compare what is created with sdist and bdist_wheel or bdist_egg; content of bdist_* is controlled by setup.py.

    So my advice is to create the following MANIFEST.in:

    prune *
    include README.txt
    recursive-include mypackage *.py