24

Suppose I have a useful python function or class (or whatever) called useful_thing which exists in a single file. There are essentialy two ways to organize the source tree. The first way uses a single module:

- setup.py
- README.rst
- ...etc...
- foo.py

where useful_thing is defined in foo.py. The second strategy is to make a package:

- setup.py
- README.rst
- ...etc...
- foo
|-module.py
|-__init__.py

where useful_thing is defined in module.py. In the package case __init__.py would look like this

from foo.module import useful_thing

so that in both cases you can do from foo import useful_thing.

Question: Which way is preferred, and why?

EDIT: Since user gnat says this question is poorly formed, I'll add that the official python packaging tutorial does not seem to comment on which of the methods described above is the preferred one. I am explicitly not giving my personal list of pros and cons because I'm interested in whether there is a community preferred method, not generating a discussion of pros/cons :)

  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Jun 4 '14 at 19:16
  • 1
    @gnat: Thanks for the link to the how to ask. I am aware of good question asking practice, but in this case I didn't think it was appropriate to give my experience because that would likely turn what I've asked as a clear "A or B" question into a solicitation for opinions. How would you recommend improving this question without making it opinion based? – DanielSank Jun 4 '14 at 19:25
  • @gnat: In other words, if I were interested in a list of pros/cons for the two methods, I would have asked "What are the pros and cons of these two methods" and would have included my own partial list. – DanielSank Jun 4 '14 at 19:31
17

You do the simplest thing that works for you.

For a one function module, there is absolutely no point in creating a package. Packages are useful for creating an additional namespace and/or for organising your code across multiple modules.

The json and unittest modules in the Python standard library are really packages for example, for code organisation purposes. But it is perfectly fine to leave code currently living in just one python file, as one module.

If you want examples:

For a good example of projects that really make excellent use of packages, look at:

There is no 'official' recommendation; both options are entirely valid.

  • So there is no officially preferred method? – DanielSank Jun 4 '14 at 19:32
  • 3
    No, there is no preferred method. – Martijn Pieters Jun 4 '14 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.