什么是distutils相当于setuptools的`find_packages`?(蟒蛇)

Tom*_*Cho 5 python install distutils setuptools setup.py

我有一个setup.py文件检查用户是否有setuptools,如果他没有,那么我被迫使用distutils.问题是,为了确保安装子模块,我使用setuptools'find package:

from setuptools import setup, find_packages
packages = find_packages()
Run Code Online (Sandbox Code Playgroud)

然后从那里开始

但是,我不知道该如何做到这一点distutils.是否有相同的功能或者我是否必须手动查找具有__init__.py内部的子目录?如果是这样的话,我是否可以要求setuptools安装我的包裹而忘记distutils

干杯.

Mar*_*ers 7

使用setuptools是完全可以接受的; PyPI上的绝大多数软件包都已经完成了.

如果你想重新发明find_packages()轮子,那么是的,查找包含__init__.py文件的目录.这就是setuptools.PackageFinder班级的作用.简化的重新实施将是:

import os
from distutils.util import convert_path


def find_packages(base_path):
    base_path = convert_path(base_path)
    found = []
    for root, dirs, files in os.walk(base_path, followlinks=True):
        dirs[:] = [d for d in dirs if d[0] != '.' and d not in ('ez_setup', '__pycache__')]
        relpath = os.path.relpath(root, base_path)
        parent = relpath.replace(os.sep, '.').lstrip('.')
        if relpath != '.' and parent not in found:
            # foo.bar package but no foo package, skip
            continue
        for dir in dirs:
            if os.path.isfile(os.path.join(root, dir, '__init__.py')):
                package = '.'.join((parent, dir)) if parent else dir
                found.append(package)
    return found
Run Code Online (Sandbox Code Playgroud)

这忽略了include和的exclude论点setuptools.find_packages().