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).
1 Answer 1
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.iois 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')")
dir1 -> dir2 -> scriptwould need to import a script indir1 -> dir2 -> dir3 -> scriptasimport dir1.dir2.dir3.scriptwhen ran from main, but will import asimport dir3.scriptwhen ran from the script in testing. Also each subfolder needs a__init__.pyin it.