简体   繁体   中英

attempted relative import with no known parent package (python)

I'm trying to import the db module from the __init__ file, after comand from. import db from. import db , gets attempted relative import with no known parent package

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app():
app = Flask(__name__)

My catalog structure:

Procjet
├── __init__.py
├── auth.py
└── templates



i tried it:

from. import db

from.. import db

from project import db

from.project import db

and always i gets attempted relative import with no known parent package

Opening the project folder directly in an IDE should fix the issue.

You could check if the correct path is in sys.path in debug mode for example.

You could try my new import library: ultraimport

It gives the programmer more control over their imports and lets you do file system based relative and absolute imports.

To import the db object relatively, your auth.py could look like this:

import ultraimport
db = ultraimport('__dir__/__init__.py', 'db')

This will always work, no matter how you run your program and independent of your sys.path.

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