mariogarcc
mariogarcc

Reputation: 388

About python package structure - elements per module

Is there any objective, strong argument against having a file for each group of correlated modules instead of heavily packaging them in a single module? For example, say I have a subpackage named utils, and one of the utilities it provides is regarding input and output of data. What I mean is if it's wrong to put big functions (with their important related subfunctions) in "private" modules and then call them via importing the main module io.

utils/
  io.py
  _pprint.py
  _ptable.py

Say _pprint is a big method that requires some other little sub-methods, then those would be in the _pprint.py module as well, because they're heavily correlated.

Basically, the issue is that I have a io.py module (and some others) with 20 functions that are a pain to read/scroll through to find some specific ones I want to change. I would like to have (as an example) an io module that can be called via import package.io and then, inside io.py, it imports the other modules that are related to I/O, or something like that (maybe I would need to do this with subpackages instead, to use more __init__.py's).

Upvotes: 0

Views: 44

Answers (1)

tgikal
tgikal

Reputation: 1680

No, as long as it is still navigable, there is nothing wrong with having a bunch of independent files.

As to your question about having a "io module that can be called via import package.io" the way to do this easily is have the directory structured:

packages -> io -> __init__.py 

In the __init__.py, have the imports for the submodules:

__init__.py:

from packages.io import submodule
  • When import packages.io is called, the submodules will be attributes of packages.io.

  • Remember the __init__.py imports need to be from the main script's perspective.

Running this should give you a layout of directories and files to play with:

import os

os.makedirs("./packages/io")
with open("./main.py", 'w') as f:
    f.write("import packages.io as io\n")
    f.write("io.module1.call()\n")
    f.write("io.module2.call()\n")

with open("./packages/io/__init__.py", 'w') as f:
    f.write("from packages.io import module1\n")
    f.write("from packages.io import module2\n")

with open("./packages/io/module1.py", 'w') as f:
    f.write("def call():\n")
    f.write("\tprint('Module 1 called')")

with open("./packages/io/module2.py", 'w') as f:
    f.write("def call():\n")
    f.write("\tprint('Module 2 called')")

Upvotes: 1

Related Questions