pythonpython-importgit-submodulesimporterror

Git submodule raises import error once used in python project


I'm using a git-submodule in my python project.

The submodule project looks like this:

-submodule_project
    - __init__.py
    - debug_util.py
    - parsing_util
        - __init__.py
        - parse.py
        - consts.py

parse.py imports debug_util.py. This structure works fine as the submodule is an independent project.

My project is built like this:

-project
    - __init__.py
    - file1.py
    - some_dir
        - __init__.py
        - main.py

So once I use the submodule as a git submodule in my project, the parse.py raises ImportError. This happens once the line which imports the debug_util.py is ran. Just to clarify: main.py imports parse.py, which imports debug_util.py

Can you explain me what i'm doing wrong, and what are the available solutions to fix this?

Here's my .gitmodules file:

[submodule "submodule_project"]
path = submodule_project
url = ../submodule_project.git

Thanks in advance for all of you!


Solution

  • Git Submodules are very annoying to work with (at least they were last time I played around with them). I'd recommend against using submodules and simply using python's own dependency management. So your submodule_project would have its own unique name and get packaged up in releases like myparser-1.2.1, and then your main project would depend on that package from its setup.py.

    Problems with git submodules (from the git documentation):

    Problems with git submodules (my own observations):

    You also don't mention how and where you set up the submodule from the top level project. It might be more helpful if you pasted the .gitmodules file from the top level project.