pyknight202
pyknight202

Reputation: 1487

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.

Upvotes: 0

Views: 38

Answers (1)

CristiC777
CristiC777

Reputation: 481

use:

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

from app1.models import ModelClassName

Upvotes: 1

Related Questions