Git submodule import statements failing
Asked Answered
D

1

3

I added a repo as a submodule to my project, but the import statements inside that repo are not resolving when I compile my project

To add the submodules, I used the command

git submodule add <git-clone-link>

I created a folder in my project called lib and added the submodule under that folder.

However, within the submodule, there are files that import from other files in the submodule. For example lets say the submodule had python packages P1 and P2. P1 has File_A and P2 has File_B.

For File_B to import File_A, the import statement looks like from P1 import File_A. This import statement should still work because both files are under the submodule.

Any thoughts on why this is not working?

Dramatization answered 13/6, 2018 at 18:23 Comment(4)
This is the wrong place to ask the question. Your issue is all about Python's import mechanism, and has nothing to do with submodules—submodules just appear as subdirectories, so all the usual Python issues with locating Python files by pathname apply.Beacham
have you tried the same imports in a standalone checkout of the repo?Dissolute
The imports in the standalone do workDramatization
I am experiencing this issue and it is because of the extra folder that you end up with due to the submodule structure.Seve
S
1

I was able to solve this by adding the git submodule folder to the python path in an __init__.py file at the top level of the parent project that includes the git submodule.

import os
import sys

# add git submodule to path to allow imports to work
submodule_name = 'submodule_123'
(parent_folder_path, current_dir) = os.path.split(os.path.dirname(__file__))
sys.path.append(os.path.join(parent_folder_path, submodule_name))
Seve answered 28/9, 2022 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.