36

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 :)

asked Jun 4, 2014 at 19:01
3
  • 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 Commented Jun 4, 2014 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? Commented Jun 4, 2014 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. Commented Jun 4, 2014 at 19:31

1 Answer 1

34

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.

answered Jun 4, 2014 at 19:30
2
  • So there is no officially preferred method? Commented Jun 4, 2014 at 19:32
  • 4
    No, there is no preferred method. Commented Jun 4, 2014 at 19:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.