Relative imports must be explicit

Imagine the following directory structure for a package named app1:

/app1 
  /__init__.py 
  /models.py 
  /tests.py  

Now, in Python 3, let's run the following in the parent directory of app1:

$ echo "import models" > app1/tests.py 

$ python -m app1.tests 
Traceback (most recent call last): 
   ... omitted ... 
ImportError: No module named 'models' 

$ echo "from . import models" > app1/tests.py 

$ python -m app1.tests 
# Successfully imported 

Within a package, you should use explicit relative imports when referring to a sibling module. You can omit __init__.py in Python 3, though it is commonly used to identify a package.

In Python 2, you can use import models to successfully import the models.py module. However, it is ambiguous ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.