Alex Rivera | Logout

python module layout

Asked 2010-11-08T01:51:23.343
15

I'm just starting to get to the point in my python projects that I need to start using multiple packages and I'm a little confused on exactly how everything is supposed to work together. What exactly should go into the __init__.py of the package? Some projects I see just have blank inits and all of their code are in modules in that package. Other projects implement what seems to be the majority of the package's classes and functions inside the init.

Is there a document or style guide or something that describes what the python authors had in mind for the use of packages and the __init__ file and such?

Edit:
I know the point of having the __init__.py file in the simplest sense that it makes a folder a package. But why would I put a function there instead of a module in that same folder(package)?

Edit
Report

1 Answer

8

__init__.py can be empty, but what it really does is make sure Python treats your directories correctly, provide any initialization you might need for when your package is imported (configuring the environment or something along those lines), or defining __all__ so that Python knows what to do when someone uses from package import *.

Most everything you need to know is described in the docs on Packages. Dive Into Python also has a piece on packaging.

answered 2010-11-08T01:59:11.770

Your Answer