Python 3.x package structure best practices

Viewed 68

I have a package that's currently structured somewhat like this:

food/
    fruit/
        __init__.py
        fruit.py # contains Fruit class
        apple.py # contains Apple class (extends Fruit)
        cherry.py # contains Cherry class (extends Fruit)

and __init__.py contains something like the following:

from .fruit import Fruit
from .apple import Apple
from .cherry import Cherry

I put the re-exports in the __init__.py file to avoid the slightly more verbose from food.fruit.apple import Apple rather than from food.fruit import Apple

I'm by no means a Python expert, so I'd appreciate some guidance from those more familiar with the best practices and "Pythonic" ways of doing things.

1 Answers

Exactly right!

Your method is perfect and is adopted by many popular libraries like numpy, pandas, flask, etc.

Good insight.

Related