8

I'm puzzled that I could not find a question on this yet, since this seems like a fairly common situation. I might have overlooked it. Similar questions (like this one) exist, but they all appear to have different goals and constraints.

I'm writing code that is using another project as a git submodule. The (simplified) situation looks like this:

.
├── A.py
└── sub
    ├── B.py
    └── C.py

The contents of the files are as follows;

A.py

import sub.B

print(sub.B.x)

B.py

import C

x = C.y * 2
if __name__ == '__main__':
    print(x)

C.py

y = 7

When I try to execute A.py, it's telling me:

  File "/Users/Joost/poc/sub/B.py", line 1, in <module>
    import C
ImportError: No module named 'C'

Naturally, it works just fine when I modify B.py to actually get C from sub.C. However, as sub is a submodule from a third party, I cannot do that. Also, it would break submodule functionality.

What is the proper way to deal with this?

10
  • Why don't you make 2 black boxes (separate projects): the sub and your project? Then, just add both projects to PYTHONPATH. Commented Oct 8, 2015 at 10:05
  • 1
    What is the reason for making sub a submodule, rather than a standard dependency? Commented Oct 8, 2015 at 10:06
  • 1
    @Joost you could fork the repository and add a setup.py, then either see if the developers want you to PR it back into the original or maintain your own installable fork. Otherwise you will have to keep it as part of your package and use relative imports to access the functionality. Commented Oct 8, 2015 at 11:10
  • 3
    Ah, I should've searched. Still, I feel like my problem stands, as I'd much prefer to submodule the original, unchanged repository than package it. Commented Oct 8, 2015 at 13:00
  • 1
    2020 and I am facing the exact same problem... I havent seen a way to preserve the submodule imports and be able to import them in the main repo Commented May 12, 2020 at 10:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.