3

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
  • 1
    Just put the __init__.py files into the directories and they will magically become packages. Don't worry, they do not byte!
    – Klaus D.
    Commented Sep 19, 2018 at 15:37

1 Answer 1

3

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.

1
  • thanks, works like a charm. I had to pip install setuptools --upgrade before.
    – ford04
    Commented Sep 20, 2018 at 4:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.