Pyproject.toml: defining script in src/main.py

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dietervansteenw
    • May 2024
    • 2

    Pyproject.toml: defining script in src/main.py

    Hi,

    I've been using the following folder layout for a while now:

    |-log # modulewith logging setup and custom handlers
    |-__init__.py
    |-my_logger.py
    |-config # folder
    |-__init__.py
    |-config.py
    |-my_module # core functionality of my program/script
    |-__init__.py
    |-my_module.py
    -main.py


    My main.py would be something like:

    Code:
    
    from config.config import Config
    from log.log import add_rotating_file, setup_logger
    from my_module import MyModule
    
    def _setup_log():
        log = setup_logger()
        add_rotating_file(log)
    
    
    def main():
        conf = Config.get_config()
    
        my_module = MyModule(conf)
        my_module.do_something()
    
    
    _setup_log()
    
    if __name__ == '__main__':
        main()
    My pyproject.toml would contain:

    Code:
    [tool.setuptools.packages.find]
    where = ['src'] 
    
    [project.scripts]
    repo_sync = 'main:main'
    I would install with
    Code:
    python -m pip install -e .
    This worked fine until I had two scripts that I use regularly and installed them globally instead of in a venv.

    This breaks things as it installs the first installed scripts "main.py" as a module "main" in the global namespace
    and is thus shared for all other scripts I set up this way.
    I thought this was not going to happen since I didn't have an __init__.py in the src directory, but that obviously isn't correct (I understand that).

    To me it makes sense to have a src directory, then general purpose libs (config/logging) and my module as subfolders and tie it all together with a main.py as above in the src directory.

    Is there a way to keep my directory structure this way and have multiple scripts installed or should I just abandon this layout?
Working...