简体   繁体   中英

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)

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. Don't do this, you are creating a circular dependency here.

Also see Circular import dependency in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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