setuptoolssetup.pydistutilspython-packaging

Customizing python package directory layout with setup.py


Suppose I have the following directory structure:

src/
└── python/
    └── generated/
        ├── __init__.py
        ├── a.py
        └── lib/
            ├── __init__.py
            └── b.py

What does my setup.py need to look like in order to create a dist with a directory layout like:

src/
└── python/
    ├── __init__.py
    ├── a.py
    └── lib/
        ├── __init__.py
        └── b.py

The goal is to simply eliminate the generated folder. I've tried endless variations with package_dir and can't get anything produced other than the original directory structure.


Solution

  • In this specific case, for this specific project directory structure, the setup.py script should be placed in the src directory and should look like this:

    #!/usr/bin/env python
        
    import setuptools
        
    setuptools.setup(
        name='Thing',
        version='1.2.3',
        packages=[
            'python',
            'python.lib',
        ],
        package_dir={
            'python': 'python/generated',
        },
    )
    

    Note the package_dir setting. It instructs setuptools to get the code for the python package from the directory python/generated. In the built distributions you will then find the right directory structure.