1

My file structure looks like this

.
├── app.py
└── tests
    ├── Test.py
    ├── Test0.py
    └── __init__.py

This is app.py:

import tests.Test

This is Test.py

import Test0

What is in Test0.py is irrelevant. Let's just say it contains nothing. __init__.py is also empty. When I run the command python3 app.py, I get the error:

ModuleNotFoundError: No module named 'Test0'

This is strange since if I change Test.py to:

from . import Test0

it all works perfectly; no errors.

I cannot understand why. I thought simply using import x would attempt to import a module named x either locally or from the standard lib.

Can somebody explain this behaviour to me? Additionally if there is a way to simply import just using import Test0 please tell me.

Python sandbox replicating issue: https://repl.it/repls/FirsthandQuickBytecode

Kyle Blue
  • 311
  • 1
  • 2
  • 11
  • 1
    How would I reproduce this problem? – nicomp Apr 14 '20 at 17:04
  • 3
    https://stackoverflow.com/questions/7279810/what-does-a-in-an-import-statement-in-python-mean – oittaa Apr 14 '20 at 17:10
  • @nicomp I have added a link in the question description to a python sandbox which replicates the problem – Kyle Blue Apr 14 '20 at 17:11
  • 1
    Try checking out [this question](https://stackoverflow.com/questions/45448182/absolute-imports-in-python-not-working-relative-imports-work) to get a better understanding of why it doesn't find the module(s). – Philip Ciunkiewicz Apr 14 '20 at 17:20
  • This relevant information can be found [in the docs](https://docs.python.org/3/reference/import.html). Basically the imported module has to be on the path for absolute imports. This is not the case if it's inside a package. – a_guest Apr 14 '20 at 19:03
  • @oittaa I see. After running this in python 2.7, it worked. So was it made a requirement to have relative imports be explicit since python 3?? – Kyle Blue Apr 14 '20 at 19:14

0 Answers0