7

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?

Community
  • 1
  • 1
Joost
  • 3,824
  • 3
  • 24
  • 47
  • Why don't you make 2 black boxes (separate projects): the `sub` and your project? Then, just add both projects to `PYTHONPATH`. – StefanNch Oct 08 '15 at 10:05
  • 1
    What is the reason for making `sub` a submodule, rather than a standard dependency? – jonrsharpe Oct 08 '15 at 10:06
  • It does not exist as a `pip` package, but merely as a bunch of module files on Github. Is there a better way to turn this into a dependency? – Joost Oct 08 '15 at 10:07
  • It doesn't need to be on PyPI necessarily, you can install from a GitHub repo using `pip` - see e.g. http://stackoverflow.com/q/8247605/3001761 – jonrsharpe Oct 08 '15 at 10:16
  • Ah, yeah, I looked into that, but all of it seems to imply adding setup files to the repository. I'm not in a position to change the repository I want to use as a submodule at all. Is it maybe possible to wrap it in a folder in my own repository to provide the necessities for a pip install? I wouldn't be able to use `pip install git+..` then, I guess.. – Joost Oct 08 '15 at 11:05
  • 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. – jonrsharpe Oct 08 '15 at 11:10
  • @jonrsharpe Suppose I went with `pip install git+..`. Is there a way to ensure that it remains at the same commit? It seems like the HEAD can easily move without changing the version, leading to breaks.. Or is that also with regular PyPi packages? I could clone as a submodule and then call `pip install -e`, but that seems to defeat the purpose a bit. – Joost Oct 08 '15 at 12:33
  • @Joost http://stackoverflow.com/q/13685920/3001761? – jonrsharpe Oct 08 '15 at 12:34
  • 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. – Joost Oct 08 '15 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 – Kailegh May 12 '20 at 10:25

0 Answers0