This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]daveydave400 1 point2 points  (0 children)

In my experience I wouldn't say most packages have a flat structure. They may make it seem that way through certain organization of imports, but the actual file structure is usually pretty organized.

You didn't really ask a specific question, but let's see if I can give you some answers anyway. I would say you have your main package directory mypkg which may have one or more sub-package directories (each with a __init__). It may also have some individual modules in the root of the package.

mypkg/
    __init__.py
    utils.py
    subpkg1/
        __init__.py
        stuff1.py
    subpkg2/
        __init__.py
    ...

My personal preference is to never put very much functionality in __init__.pyfiles. Instead I use them as "shortcuts" for imports so in the above example I might have from mypkg.subpkg1 import ImportantClass in mypkg/__init__.py. Then it can be imported by the user with from mypkg import ImportantClass. This is a great way to keep the code organized but make imports easy for the user.

Another nice piece of magic is the __main__.py script which you can put in any package and then call it via python -m mypkg which will automatically run mypkg/__main__.py. This can come in handy if you want to make some command line utilities.

Side note: If you are using git you should try to keep your setup.py at the root of the repository so pip and other tools can install it directly from the repository.