Thursday, October 25, 2012

Python Project Directory Structure

The "proper" way to setup a Python project is (unfortunately) subject to some debate (just check out these Stackoverflow threads here, here, and here or search for "python project directory structure"). One of the biggest points of contention is where to put the unit tests -- should they go under the project source tree or should the be separate?

Although arguments can be made for either organization structure, I feel that following the lead of the Django project, and separating the tests from the rest of the source code is much cleaner. Doing so clearly differentiates between the actual code of the project and the testing of that code (which can be thought of as a consumer of the project code). Also, make sure not to call the directory "test" as there is an internal Python module named test.

MyProject/
    setup.py
    myproject/
        __init__.py
        somemodule.py
        somepackage/
            __init__.py
            foo.py
    docs/
    tests/
        runtests.py
        unittests/
            __init__.py
            ...
        integrationtests
            __init__.py
            ...

I'm sure that there are many who will say that I'm wrong, but at least I can fall back on the fact that Django does it this way. If you don't believe me, just check out their source code.

No comments:

Post a Comment