简体   繁体   中英

Python relative import beyond top-level package

project
 +
 +--------+app1
 |          +
 |          +-------------+models.py  <---+
 |                                        |
 |                                        |
 |                                        | from ..app1.models import Model
 +--------+app2                           |
 |         +                              |
 |         +--------------+models.py      +
 |
 +

I want to import the Model class in module models from app1 into app2. When attempting to use from..app1.models import Model , I receive the error Attempted relative import beyond top-level package .

use:

import sys
sys.path.append("..")            # or sys.path.append(".")
print (os.getcwd())

from app1.models import ModelClassName

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