0

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

asked Feb 13, 2019 at 15:03
2
  • No, as long as it is still navigable there is nothing wrong with having a bunch of independent files. The only thing to remember is imports are seen from the perspective of the main script so a script in dir1 -> dir2 -> script would need to import a script in dir1 -> dir2 -> dir3 -> script as import dir1.dir2.dir3.script when ran from main, but will import as import dir3.script when ran from the script in testing. Also each subfolder needs a __init__.py in it. Commented Feb 13, 2019 at 16:06
  • @tgikal Thank you. Don't be afraid to post that as an answer. Commented Feb 13, 2019 at 16:21

1 Answer 1

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.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')")
answered Feb 13, 2019 at 16:29
Sign up to request clarification or add additional context in comments.

Comments

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.