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?

8
  • have you looked at the documentation on packages? Commented Mar 27, 2016 at 21:39
  • @TadhgMcDonald-Jensen - yes, but alas it doesn't really clear things up or help. I've also read: stackoverflow.com/questions/2699287/what-is-path-useful-for and stackoverflow.com/questions/31809747/… and stackoverflow.com/questions/448271/what-is-init-py-for Commented Mar 27, 2016 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 Commented Mar 27, 2016 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. Commented Mar 27, 2016 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. Commented Mar 27, 2016 at 22:08

0

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.