简体   繁体   English

如何处理Python中子模块的相互依赖性?

[英]How to deal with submodule interdependency in Python?

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 aa git submodule of its parent. 其中每个子模块都是其父级的git submodule

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 clone ing and git submodule add ing it. 这样做的好处是我可以独立管理和开发每个子模块,并且将这些子模块之一添加到新项目中就像git clone ing和git submodule add它一样简单。 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. 缺点是,在此示例中,我现在拥有3个tools子模块副本,它们彼此独立,并且每当它们中的任何一个发生更改时都必须手动进行更新。 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. 同样,由于每个子模块都运行测试,并且tools模块有3个副本,因此它现在使我运行的单元测试的数量增加了两倍。

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. 我已经在此处阅读了有关如何创建正式的Python程序包的信息,但这似乎是一项艰巨的任务,并且会使最终用户实际安装和运行该程序更加困难。

Another relevant question asked here 在这里问另一个相关的问题

Better to have a single tool in the parent and import it to the submodule. 最好在父级中只有一个工具,然后将其导入子模块。 that's by far feel best to me. 到目前为止对我来说感觉最好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM