Django ImportError
Solution 1
It turns out that it was a circular import error. models.py
was importing a.py
and a.py
was importing models.py
.
Breaking this circle solved the import issues, but now it is time to figure out how to do lazy evaluation in Python :-)
Solution 2
Put empty file named: __init__.py
inside /submodule1 so Python can parse it as module. It should help. The same in /appname - there should be also __init__.py
file
Andrew Lee
Updated on July 19, 2022Comments
-
Andrew Lee almost 2 years
I created my own Django application with directory structure
/appname __init__.py models.py /submodule1 __init__.py a.py
Inside
a.py
I have the following importfrom ..models import Something
This works okay if I have
/appname
inside my/djangoproject
folder, but when I install the app to Python's site-packages (viasetup.py
which I created), all hell breaks loose anda.py
can no longer importSomething
, with the following error:ImportError: cannot import name Something
Here is the setup.py:
from distutils.core import setup setup(name='appname', version='0.1', packages=['appname', 'appname.contrib'], )
-
Andrew Lee over 12 yearsYes I had
__init__.py
in both/submodule1
and/
but it is still giving me theImportError
. -
Harry over 10 yearsI've run into this myself, I just put the import statement inside the function that required
a.py
but that seems icky. What was your solution?