Proper way to structure a program formed by 3 sub-programs

Viewed 108

I'm building a system formed by 3 programs, lets call them A, B and C. Right now I think that my file structure is a disaster.

Inside my root folder I have some shortcuts to .bat files that run the programs and a folder called programs_data. Inside that folder I have 4 separate folders, one for each program plus a common folder. The issue is that I need each program and the sub-scripts in those programs to be able to import from the common folder and I need program C and B to be able to call program A API.

As it is right now, I have a mess of appending to sys.path in the sub files to import functions from upper levels.

What's the correct way to structure something like this?

Current structure:

root
├── Configuration.lnk
├── Documentation.lnk
├── Program A.lnk
├── Program B.lnk
├── Program C.lnk
├── programs_data
│   ├── Program A
│   │   ├── Program A API.py
│   │   ├── Program A.bat
│   │   ├── Program A.py
│   │   ├── src
│   │   │   ├── server.py
│   │   │   ├── test_functions.py
│   │   │   └── validation.py
│   │   └── targets
│   │       ├── sql_querys
│   │       │   ├── query1.sql
│   │       │   ├── query2.sql
│   │       │   └── queryn.sql
│   │       ├── target1.py
│   │       ├── target2.py
│   │       ├── target3.py
│   │       └── targetn.py
│   ├── Program B
│   │   ├── Program B.bat
│   │   ├── Program B.py
│   │   ├── classifiers
│   │   │   ├── classifier1.py
│   │   │   ├── classifier2.py
│   │   │   └── classifiern.py
│   │   ├── events.log
│   │   ├── o365_token.txt
│   │   └── src
│   │       ├── batchECImport.py
│   │       ├── classifier.py
│   │       └── logger.py
│   ├── Program C
│   │   ├── Program C.bat
│   │   ├── Program C.py
│   │   ├── Reports
│   │   │   ├── report 1
│   │   │   │   └── report.py
│   │   │   └── report 2
│   │   │       └── report.py
│   │   ├── o365_token.txt
│   │   ├── schedule.xlsx
│   │   └── src
│   │       └── report.py
│   └── common
│       ├── APIMailboxManager.py
│       ├── Documentation
│       │   └── Documentation.pdf
│       ├── FlexibleProcess.py
│       ├── config.py
│       ├── misc.py
│       ├── print_functions.py
│       ├── production_support_passwords.py
│       ├── reports_log.db
│       └── reports_log.py
└── schedule spreadsheet.Ink

Thanks!

2 Answers

You could use __init__.py files to configure the import path of your modules in each directory. Documentation here. In these files, you should only add the relative path to the common folder. I don't think there is an other way to have a common folder if you want to avoid duplicated code...

Related