2

I've created a program with two stand-alone components (app1 and app2 below), that are both stand-alone but share a couple of basic modules (stored in app1). I'm now trying to merge these two apps together into one application. My application has a structure like this:

/root
    |-docs
    |-config
    |-app1
    |   |-src
    |   |   |-main.py
    |
    |-tests
    |    |-a_tests.py
    |-app2
    |    |-API
    |    |-web-server.py
    |    |    |-REST.py

I have a few requirements:

  • /app2/API/REST.py and its kin needs to be able to read modules from /app1/src/
  • /tests/ needs to be able to read modules from both app1 and app2. The tests are currently all written for /app1/, but I've since moved app1 into a directory deeper and thus they've all stopped working.

I know I may need to use some __init__.py files (/root/ and all the directories with python files in have an empty one presently) and I've tried __path__ too, but I keep getting ImportError: No module named <main.py> errors. I note this question - Relative import in Python 3 not working - and when I try to use a relative import from app2 to app1, I get that error message. But that question only says I need to use absolute imports, not how to do them.

How do I accomplish this?

Community
  • 1
  • 1
GIS-Jonathan
  • 4,347
  • 11
  • 31
  • 45
  • have you looked at the [documentation on packages](https://docs.python.org/3.5/tutorial/modules.html#packages)? – Tadhg McDonald-Jensen Mar 27 '16 at 21:39
  • @TadhgMcDonald-Jensen - yes, but alas it doesn't really clear things up or help. I've also read: http://stackoverflow.com/questions/2699287/what-is-path-useful-for and http://stackoverflow.com/questions/31809747/python-imports-with-init-py and http://stackoverflow.com/questions/448271/what-is-init-py-for – GIS-Jonathan Mar 27 '16 at 21:41
  • ok, when one module tries to import another module in the package, are you executing it as the main program? you would need to run a program outside of the package for relative imports to work – Tadhg McDonald-Jensen Mar 27 '16 at 21:42
  • @TadhgMcDonald-Jensen - app2 isn't run as the main program; they're modules for bottle. App1 is run as a main program. Tests are pytest, so I guess they're not run as main program either. – GIS-Jonathan Mar 27 '16 at 21:45
  • ok, so `root/app1/src/main.py` is running as main, what are you tying to import and how? `from ... import app2.src.main` would work if it wasn't running as main but it is more complicated if a submodule is the main executable. – Tadhg McDonald-Jensen Mar 27 '16 at 22:08
  • @TadhgMcDonald-Jensen - app1 isn't trying to import anything; it's effectively self-contained. App2 needs to import a couple of modules from app1. The /tests/ of course need to be able to import from both apps. App1 isn't a problem to run, it's app2 and the tests that are. – GIS-Jonathan Mar 27 '16 at 22:11
  • when you execute your program, what module is being executed? Which one will have `__name__ == "__main__"` is the information I need, if it is outside of the entire package structure then relative imports are all you need. – Tadhg McDonald-Jensen Mar 27 '16 at 22:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107492/discussion-between-gis-jonathan-and-tadhg-mcdonald-jensen). – GIS-Jonathan Mar 27 '16 at 22:31

0 Answers0