Search code examples
pythonsetup-projectdirectory-structure

Python: Project with multiple packages, but I'd like to have one config file


I have a project that I will distribute that will contain several sub-packages. One for cleaning data, one for transforming data, and another for calculating some statistics. I'd like to avoid having a 3 config.py files, can I just put a config.py file in the top level? For example, instead of doing this:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   ├── config.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   ├── config.py
|     |   └── g1.py
|     |
|     └── stat_calc
|        ├── __init__.py
|        ├── config.py
|        ├── s1.py
|        └── command_line_interface.py # <- users will use this from cmd.exe
|      
├── README.txt    
|
└── setup.py

I would like to have:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── config.py  # <---- put all 3 config.py stuff into here
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   └── g1.py
|     |
|     └── stat_calc
|         ├── __init__.py
|         ├── s1.py
|         └── command_line_interface.py # <- users will use this from cmd.exe
| 
├── README.txt    
|
└── setup.py

Would this make sense and is this pythonic/correct?


Solution

  • Assuming your module will be stored in the PYTHONPATH, the answer is yes. Just import it:

    from MyPackage import config