简体   繁体   English

相对导入在Python中不起作用

[英]relative import does not work in Python

I have a project structure like this... 我有这样的项目结构......

app/
    main.py
    app/
        __init__.py
        boot.py
        server.py
        controllers/
            __init__.py
            home.py

The imports are... 进口是......

# main.py
from app import server

# server.py
from . import boot

# boot.py
from . import controllers

# controllers/__init__.py
from . import home

# controllers/home.py
from .. import boot

Now all the imports are working except the last one . 现在所有的导入都在工作, 除了最后一个 The error thrown is... 抛出的错误是......

ImportError: cannot import name boot

What's the problem? 有什么问题? (I am using Python 3.2) (我使用的是Python 3.2)

You are importing boot which is importing controllers , which is then asked to import home , and home then tries to import boot , but it wasn't done importing yet. 您正在导入正在导入controllers boot ,然后要求导入home ,然后home尝试导入boot ,但尚未导入。 Don't do this, you are creating a circular dependency here. 不要这样做,你在这里创建一个循环依赖。

Also see Circular import dependency in Python 另请参阅Python中的循环导入依赖项

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

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