Skip to content

Python Setuptools Setup

DodaTech 1 min read

In this tutorial, you'll learn about Python Setuptools Setup Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

error: Multiple top-level packages discovered in a flat-layout: ['src', 'tests'].

Setuptools 61+ warns about ambiguous flat-layout packages.

Wrong

.
├── setup.py
├── src/
│   └── mypackage/
└── tests/

Running python setup.py sdist outputs a warning about flat layout.

Use a pyproject.toml configuration:

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "mypackage"
version = "0.1.0"

Or configure setuptools explicitly in setup.py:

from setuptools import setup, find_packages

setup(
    name="mypackage",
    version="0.1.0",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
)

Expected output: package builds without layout warnings.

Prevention

  • Use pyproject.toml instead of setup.py for new projects
  • Use find_packages(where="src") with a src/ layout
  • Pin setuptools version for reproducible builds

Common Mistakes with setuptools setup

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between setup.py and pyproject.toml?

setup.py is the legacy configuration file. pyproject.toml is the modern PEP 621 standard for Python project metadata. New projects should use pyproject.toml.

Why does setuptools warn about flat layout?

Setuptools 61+ defaults to flat-layout detection which may include unwanted directories. Use explicit packages configuration to avoid this.

How do I create a source distribution?

Use python -m build --sdist (recommended) or python setup.py sdist (legacy). The result is a .tar.gz file in the dist/ directory.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro