Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make distutils in Python automatically find packages

People also ask

What is distutils package Python?

The distutils package provides support for building and installing additional modules into a Python installation. The new modules may be either 100%-pure Python, or may be extension modules written in C, or may be collections of Python packages which include modules coded in both Python and C.

Is Distutils deprecated?

In Python 3.10 and 3.11, distutils will be formally marked as deprecated. All known issues will be closed at this time. import distutils will raise a deprecation warning. New issues that would be considered release blocking may still be fixed, but support for new tools or platforms will not be added.

What can I use instead of distutils?

Being outside the standard library, setuptools also offers a more consistent feature set across different versions of Python, and (unlike distutils ), setuptools will be updated to produce the upcoming “Metadata 2.0” standard formats on all supported versions.


I would recommend using the find_packages() function available with setuptools such as:

from setuptools import setup, find_packages

and then do

packages=find_packages()

The easiest way (that I know of) is to use pkgutil.walk_packages to yield the packages:

from distutils.core import setup
from pkgutil import walk_packages

import mypackage

def find_packages(path=__path__, prefix=""):
    yield prefix
    prefix = prefix + "."
    for _, name, ispkg in walk_packages(path, prefix):
        if ispkg:
            yield name

setup(
    # ... snip ...
    packages = list(find_packages(mypackage.__path__, mypackage.__name__)),
    # ... snip ...
)

Same as dm76 answer, just that I also have tests in my repo, so I use:

from setuptools import find_packages

packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),

import re, os
def find_packages(path='.'):
    ret = []
    for root, dirs, files in os.walk(path):
        if '__init__.py' in files:
            ret.append(re.sub('^[^A-z0-9_]+', '', root.replace('/', '.')))
    return ret

setup(
...
packages = find_packages()
...
)