wisenickel
wisenickel

Reputation: 394

Local imports work in bundled PyInstaller app but in Python source

This issue has plagued me for the last few months, I need a more experienced opinion. We have a CLI Python application that uses a gRPC server to communicate with other backend services. Its structured something like this:

app
    - gRPC_service
        - __init__.py
        - service_related_files.py
        - service_tests
            - __init__.py
            - test_service.py
    
    - src
        - __init__.py
        - src_files.py

    - python-win
        - python37.dll

    - gRPC_service.spec

A few notes about the application:

The number one issue I have faced when developing this application is namespace and importing issues, and I'm not exactly sure what's causing it. We have a sub-application within the src directory that only imports modules from within the src package and uses 3rd party libraries. This works just fine with no ModuleNotFound errors.

Issues begin to surface when importing src modules from within the gRPC_service package. Even though both packages have __init__.py files, ModuleNotFound errors will be thrown if the PYTHONPATH is not modified at runtime. A work-around solution to this is to collect all the different file paths to each package within app and add them to sys.path. It goes without saying this is inconvenient.

This works for importing a majority of the modules, but to add to the confusion, some of the modules from the src package can only be imported after modifying sys.path AND adding a src. prefix to all of the local imports within the src package. Adding this prefix to local imports breaks the sub-application in the src package that I was speaking of earlier. A 'src can't be found' error gets thrown in the sub-app when doing this.

Additionally, without adding the src. prefixes to corresponding imports, no ModuleNotFound errors are thrown when the gRPC_service is bundled as a PyInstaller .exe. I have modified the pathex within the .spec file. The app works just fine when bundled - how do I get equivalent behavior when just running Python from source?

So I am looking for some advice from Python devs who have worked on large code bases. Is this issue common? What can I do to alleviate the inconvenience of modifying the PYTHONPATH at runtime? Is there a fix-all solution that tends to the needs of both applications within this codebase?

Upvotes: 1

Views: 85

Answers (1)

Alexander
Alexander

Reputation: 17291

I can think of two solutions,

  1. Split gRPC_services into its own project and repository and add it as a dependency of this project and install and import it like any other third party library.

  2. move the gRPC_services folder inside of the src folder as a subpackage.

Upvotes: 1

Related Questions