使用setup.py自定义python软件包目录布局

问题描述:

假设我具有以下目录结构:

Suppose I have the following directory structure:

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

我的 setup.py 需要什么样子才能创建具有如下目录布局的dist:

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

目标是简单地消除 generate 文件夹.我用 package_dir 尝试了无尽的变化,除了原始目录结构之外,什么也无法产生.

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.

您的 setup.py 应该放在您的 src 目录中,并且应如下所示:>

Your setup.py should be placed in your src directory and should look like this:

#!/usr/bin/env python3

import setuptools

setuptools.setup(
    name='Thing',
    version='1.2.3',
    packages=[
        'python',
        'python.lib',
    ],
    package_dir={
        'python': 'python/generated',
    },
)

请注意 package_dir 设置.它指示 setuptools 从目录 python/generated 中获取 python 包的代码.在内置发行版中,您将找到正确的目录结构.

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.