0

I am getting an error in my project where I cannot import my code. I am not sure why this is happening, as I've never run into this issue before.

I have consulted these questions:

ModuleNotFoundError: What does it mean main is not a package?

ModuleNotFoundError: No module named 'main.xxxx'; 'main' is not a package

No module named 'main.demo'; 'main' is not a package python3

My project's setup looks like this:

-code
    -__init__.py
    -my_module.py
    -driver.py
-tests
    -__init__.py
    -test_my_module.py

When trying to execute the following code:

from .my_module import MyModule

if __name__ == '__main__':
    my_mod = MyModule()

I get the following error:

Traceback (most recent call last):
  File "driver.py", line 1, in <module>
    from .my_module import MyModule
ModuleNotFoundError: No module named '__main__.my_module'; '__main__' is not a package

I am running this when in the code directory. I have tried the following imports as well:

from code.my_module import MyModule
from ..my_module import MyModule
import my_module.MyModule
import MyModule

Any help is greatly appreciated.

Tyler
  • 418
  • 5
  • 18
  • Is the code you're try to execute `driver.py`? If not, where is it located relative to your project's setup? – martineau Feb 08 '20 at 00:24
  • Yes, that is the code I am trying to execute. It is in the code directory. – Tyler Feb 08 '20 at 01:56
  • Since `my_module.py` is in the same directory as `driver.py`, I think all you need is `from my_module import MyModule` — assuming there's something named `MyModule` defined in `my_module.py`. – martineau Feb 08 '20 at 07:22

0 Answers0