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?

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. Commented Jun 13, 2018 at 18:41
  • have you tried the same imports in a standalone checkout of the repo? Commented Jun 13, 2018 at 21:03
  • The imports in the standalone do work Commented Jun 14, 2018 at 5:38
  • I am experiencing this issue and it is because of the extra folder that you end up with due to the submodule structure. Commented Sep 28, 2022 at 16:39

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.