0

Similar to diamond inheritance problem with classes, I have a problem similar to importing modules in Python. I have,

'main.py' outside the folder 'mypackage'.

#!/usr/bin/env python3

import mypackage.display_functions as displayFunctions
import mypackage.computations      as comp

if __name__=="__main__":
    a = comp.do_some_computations()
    displayFunctions.do_some_work(a)

where computations.py and display_functions.py are located in mypackage. I am not sure if I need __init__.py?

#!/usr/bin/env python3

import display_functions as displayFunctions

class computations:
    def do_some_computations():
        pass


if __name__=="__main__":
    C = computations()
    a = C.do_some_computations()
    displayFunctions.do_some_work(a)

'displayFunctions.py'

#!/usr/bin/env python3
import matplotlib

def do_some_work(a):
    pass

python3 computations.py works while doing python3 main.py it complains about import mypackage.computations as comp cannot import display_functions in file computations.py. Basically,

from mypackage.computations as comp
  File "~/mypackage/computations.py", line X, in <module>
    import display_functions as displayFunctions
ModuleNotFoundError: No module named 'display_functions'

I want to use display_functions.py in computations.py as I want to locally test and display some stuff in there i.e. python3 computations.py should run on its own and pass the test return in its __name__ part.

How do I make this happen in a simple, non-intrusive way?

Sowmya
  • 91
  • 1
  • 9
  • If `main.py` is also inside the `mypackage` folder then you can do it just like `import display_functions as displayFunctions`. The same thing for `computations.py`, you can remove `from .` part. Please note that `comp.do_some_computations()` has to be called through class `computations` (you should give it capital `C`) not only through module (file) `computations.py` – sstevan May 11 '20 at 18:34
  • 'main.py' is outside the 'mypackage'. Sorry, I forgot to mention that it is the 'display_functions.py' that is just a collection of functions and no 'class' while 'computations.py' has a class 'computations' – Sowmya May 11 '20 at 18:35
  • Adding empty `__init__.py` inside `mypackage` folder might solve it. – sstevan May 11 '20 at 18:45
  • Adding an empty `__init__.py` didn't help :( – Sowmya May 11 '20 at 18:47
  • If ``display_functions`` and ``computations`` are part of a package, then they *have* to import each other as ``import mypackage.computations`` or ``from . import computiatons``. Otherwise, it's not a package but just a folder in which two files are (and ``main.py`` has to handle them accordingly). You should not treat ``mypackage`` as *both* a ``package`` and directory at the same time. – MisterMiyagi May 11 '20 at 18:47
  • It means I was correct to use `from .` or `mypackage.filename1` in `filename2` – Sowmya May 11 '20 at 18:49
  • After jumping over few links this helped https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python/8195271#8195271 – Sowmya May 11 '20 at 18:55
  • How do I do unit testing now? for all the packages in mypackage? – Sowmya May 11 '20 at 18:57

0 Answers0