0

My first question on stack! :D

I have a problem when I try to have my cake and eat it too, it seems. I have stripped out all the code as I do not believe that it is part of the problem.

I have the following dir structure:

master/
    - main.py
    - modules/
        - parent_class.py
        - child_class.py
        - __init__.py

In parent_class.py:

class Parent:
    pass

In child_class.py:

from modules.parent_class import Parent

class Child(Parent)
    pass

if __name__ == "__main__":
    child = Child()
    child.do_stuff()

In main.py:

from modules.child_class import Child

child = Child()

child.do_stuff()

The problem I am having I believe it has to do with me not understanding sys.path properly.

When I run main.py there are no errors. However, when I try to run child_class.py for testing purposes I get the following error...

Traceback (most recent call last):
  File "child_class.py", line 1, in <module>
    from modules.parent_class import Parent
ModuleNotFoundError: No module named 'modules'

The error goes away when I change child_class.py to this:

from parent_class import Parent

class Child(Parent)
    pass

if __name__ == "__main__":
    child = Child()
    child.do_stuff()

But now when I run main.py I get this error:

Traceback (most recent call last):
  File "c.../main.py", line 1, in <module>
    from modules.child_class import Child
  File "...\child_class.py", line 1, in <module>
    from parent_class import Parent
ModuleNotFoundError: No module named 'parent_class'

How do you do a unit test if you have to change the import line every time? Thank you in advance for a good explaination. (I have read a lot of docs on importing, and packages and modules, watched like 10 different vids on this topic but still not sure why or how to make this just work.) (I am just saying I have tried to find the answer, but I am now exhausted and need a solution before I really go mad!) thank you, thank you, thank you

  • Does this answer your question? [Import a module from both within same package and from outside the package in Python 3](https://stackoverflow.com/questions/47319423/import-a-module-from-both-within-same-package-and-from-outside-the-package-in-py) – MisterMiyagi Nov 23 '21 at 12:07
  • Yes, that is what I found out later via another source. I forget where. This was a real pain to figure out. I will write a better answer to my question below. Thanks for you help though! – sageninjahawk Nov 25 '21 at 04:46

2 Answers2

0

Until someone shows me a better way, I will do the following...

if __name__ == "__main__":
  from parent_class import Parent
else:
  from modules.parent_class import Parent

0

TLDR:

Run the python file with the -m flag.

python -m modules.child_class


This issue is a result of misunderstanding the difference between Python Script programs and Python Package programs.

If you are running a Python program as a script (running a Python file directly), then you do direct imports like you have already:

from parent_class import Parent

However, if you are building a Python Package that is designed for importing into other programs (eg. a Library or Framework) then you need to use relative imports. (Sounds like the Unittest is running the program as a package but when you run the program, you are running it as a script.)

    from .parent_class import Parent
# or
    from modules.parent_class import Parent

If you are making a package then run the program with the -m flag or import into another program (a script or another package)

python -m main

or

python -m modules.child_class

elec2018au
  • 16
  • 1