Organising my Python project

Create an __init__.py file in your projects folder, and it will be treated like a module by Python.

Classes in your package directory can then be imported using syntax like:

from package import class
import package.class

Within __init__.py, you may create an __all__ array that defines from package import * behavior:

# name1 and name2 will be available in calling module's namespace 
# when using "from package import *" syntax
__all__ = ['name1', 'name2'] 

And here is way more information than you even want to know about packages in Python

Generally speaking, a good way to learn about how to organize a lot of code is to pick a popular Python package and see how they did it. I’d check out Django and Twisted, for starters.

Leave a Comment