简体   繁体   English

Django ImportError

[英]Django ImportError

I created my own Django application with directory structure 我用目录结构创建了自己的Django应用程序

/appname
    __init__.py
    models.py
    /submodule1
        __init__.py
        a.py

Inside a.py I have the following import a.py里面我有以下导入

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: 如果我在/djangoproject文件夹中有/appname这可以正常工作,但是当我将应用程序安装到Python的site-packages(通过我创建的setup.py )时,所有地狱都会破坏, a.py无法再导入Something ,以下错误:

ImportError: cannot import name Something

Here is the setup.py: 这是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 . models.py正在导入a.pya.py正在导入models.py

Breaking this circle solved the import issues, but now it is time to figure out how to do lazy evaluation in Python :-) 打破这个圈子解决了导入问题,但现在是时候弄清楚如何在Python中进行延迟评估:-)

Put empty file named: __init__.py inside /submodule1 so Python can parse it as module. 将名为: __init__.py空文件放在/ submodule1中,以便Python可以将其解析为模块。 It should help. 它应该有所帮助。 The same in /appname - there should be also __init__.py file 在/ appname中相同 - 应该还有__init__.py文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM