What is the smallest possible layout for a simple Python library?

Viewed 43

If sharing a Python library via PyPI, so that someone else can download it with pip and import it in their code, the minimal layout that I know of is this:

+ foo
  + src
    + bar
      __init__.py
      baz.py
  setup.py

I am ignoring the potential presence of setup.cfg, README, requirements.txt and so on. The names foo and bar are typically the same, sometimes even baz, but I'm distinguishing them here for clarity.

It is possible to remove src to reduce it further:

+ foo
  + bar
    __init__.py
    baz.py
  setup.py

Is it possible to achieve an even smaller layout for trivial (but still useful!) libraries? A live example would be ideal, but documentation, a blog post or a working example would be more than adequate. Alternatively, a clear explanation as to why this is not achievable is also welcome.

I'm imagining something like...

+ foo
  baz.py
  setup.py

or the possibly insane...

+ foo
  setup.py

(although my hunch is that this last is not even achievable)

1 Answers

This is covered in the distutils documentation as Pure Python distribution (by module) (thanks @phd for the link.):

If you’re just distributing a couple of modules, especially if they don’t live in a particular package, you can specify them individually using the py_modules option in the setup script.

With the following example (adapted to match the examples given in the original question):

Layout

+ foo
  baz.py
  setup.py

setup.py

from distutils.core import setup
setup(name='biff',
      version='1.0',
      py_modules=['baz'],
      )

Note that this is legacy documentation 1 2 but still applicable because the layout remains the same. We can assume the documentation will be moved and updated at some point in the future.

Related