Is there a way to create a source distribution incl. subpackages without __init__.py?

Viewed 593

Is it possible to create a source distribution with python setup.py sdist without having to use some sort of __init__.py files in every (sub)package (python 3.5+)? I really would like to just use namespace packages to avoid any redundancy and overhead. All nested .py-files in all subpackages should be included.

So take the following project structure (I tried to be analogous to pytest documentation):

project_structure

setup.py:

from setuptools import setup, find_packages
setup(
    version="0.0.1",
    name="py_import_test",
    package_dir={"": "src"},
    packages=find_packages(where='src')
)

In main.py I use something like

import py_import_test.p1.helloworld
# do something with helloworld-module

When I python setup.py sdist with the above project structure, I get no source files at all in the resulting .tar.gz. It only works, if there is __init__.py under py_import_test, p1 and possible further subpackages.

All in all, my goal is to have some standalone application package, that I can use to test against and easily install my package to a remote server. For this purpose, I would like to have the source tree as lean as possible and utilize up-to-date python packaging patterns.

Apart from my question regarding __init__.py : Would the procedure above be considered a good/modern practise?

Greetings

1 Answers

Instead of find_packages, you want find_namespace_packages (previously known as find_packages_ns).

A recent version of setuptools is required; the function was added in version 40.1.0, released in August 2018.

You also need to be careful that you don't include directories that aren't intended to be packages, but the where='src' that you already have should take care of that.

Related