开发者

Django ImportError

开发者 https://www.devze.com 2023-03-29 01:59 出处:网络
I created my own Django application with directory structure /appname __init__.py models.py /submodule1 __init__.py

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 import

from ..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 (via setup.py which I created), all hell breaks loose and a.py can no longer import Something, 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'],
)


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 :-)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消