'File Inside Python Module Not Included (Django, Py2 to Py3 Conversion)

I am migrating a Django project from 2 to 3 and am running into an import(?) error.

One of the apps/modules contains an __init__.py, admin.py, forms.py, models.py, urls.py, and view.py, but when the module is imported/created only admin, forms, and models are a part of it.

A dir of the module looks like this:

['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 'admin',
 'forms',
 'models']

If I try something like import app.viewsor from . import views, I get a SyntaxError.



Solution 1:[1]

Those files have been imported in some other part of your code, they are added to the module when imported. When Django starts up it will load several modules automatically, things like your models to populate a registry/cache of all models in your apps

>>> import app
>>> dir(app)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'admin', 'apps', 'models']
>>> import app.views
>>> dir(app)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'admin', 'apps', 'models', 'views']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iain Shelvington