1

I have a program with several submodules. I want the submodules to be usable independently, so that I can use some of them in other programs as well. However, the submodules have inter-dependency, requiring aspects of each other to run. What is the least problematic way to deal with this?

Right now, I have structured my program like this:

myapp/
|-- __init__.py
|-- app.py
|-- special_classes
|   |-- __init__.py
|   `-- tools
|       `-- __init__.py
|-- special_functions
|   |-- __init__.py
|   `-- tools
|       `-- __init__.py
`-- tools
    |-- __init__.py
    |-- classes.py
    `-- functions.py

Where each submodule is a a git submodule of its parent.

The advantage of this is that I can manage and develop each submodule independently, and adding one of these submodules to a new project is as simple as git cloneing and git submodule adding it. Because I work in a managed, shared computing environment, this also makes it easy to run the program since user environment management & software versioning and installation are contentious issues.

The disadvantage is that in this example, I now have 3 copies of the tools submodule, which are independent of each other and have to be manually updated everytime there is a change in any of them. Doing any sort of development on the submodules becomes very cumbersome. Similarly, it has now tripled the number of unit tests I run, since tests get run in each submodule and there are 3 copies of the tools module.

I have seen various importing methods, such as those mentioned here but that does not seem like an ideal solution for this.

I have read about how to create a formal Python package here but this seems like a large undertaking and will make it much more difficult for my end users to actually install and run the program.

Another relevant question asked here

user5359531
  • 3,217
  • 6
  • 30
  • 55
  • "I want the submodules to be usable independently" — Independently of the program or each other? – jwodder Aug 23 '17 at 16:36
  • @jwodder both. I have several other programs that also will be using some or all of the submodules. – user5359531 Aug 23 '17 at 16:37
  • If the submodules have interdependencies on each other, they *can't* be used independently of each other, by definition. Either combine the submodules into one repository or rewrite them so that they don't use each other. – jwodder Aug 23 '17 at 16:40
  • 1
    Creating a formal python package really isn't' that bad. Many of those fields are optional and it makes it easier for end users to install, since they just need to check out the repo and do a `pip install .` – Doryx Aug 23 '17 at 16:42
  • The interdepency has been internalized by including hard-copies of all dependencies within each submodule. That is why `special_classes` contains its own `tools` even though the parent app also has `tools`. – user5359531 Aug 23 '17 at 16:43
  • @Doryx I do not disagree, but requiring user-installation creates a lot of headaches on our systems because there are six different Python modules, only one or two of which will be compatible, not counting user-installed configurations. It also creates a lot more configuration requirements for things like compute cluster job submission, and `cron` automation. Right now, things "just work", at the expense of development headaches. – user5359531 Aug 23 '17 at 16:46

1 Answers1

1

Better to have a single tool in the parent and import it to the submodule. that's by far feel best to me.

  • I am trying this out by [adding the parent dir to the `sys.path`](https://stackoverflow.com/a/11158224/5359531), though it has caused issues with other submodules with the same name being located in both locations. Not sure yet if I will stick with this method. – user5359531 Aug 24 '17 at 16:34
  • In your proposed file structure, you have 3 tools and those are independent of each other. then you can rename your tools in the special class and function. or you need to figure out a way to convert your interdependent submodules to independent submodules. – Ghostranger Aug 25 '17 at 08:32