ImportError : Attempted relative import with no known parent package

瘦欲@ 提交于 2020-07-08 11:22:23

问题


I am learning to program with python and I am having issues with importing from a module in a package. I have tested commenting off the jedi enabled part and it is not working. I am using the visual studio code with Python 3.8.2 64 bit.

My Project Directory

.vscode
├── ecommerce
│   ├── __init__.py
│   ├── database.py
│   ├── products.py
│   └── payments
│       ├── __init__.py
│       ├── authorizenet.py
│       └── paypal.py
├── __init__.py
└── main.py

in the products.py file i have written:

#products.py
from .database import Database
p = Database(3,2)

So that I can import the Database class from the database.py module

Any help will be appreciated


回答1:


Since you are using Python 3.8 version, the imports work a little differently, but I think this should work:

Use either:

from database import Database
#Database is the class

or try:

import database.Database

lastly, this one is very secure and best practice possibly:

from . import Database  
# The '.' (dot) means from within the same directory as this __init__.py module grab the Database class.


来源:https://stackoverflow.com/questions/60593604/importerror-attempted-relative-import-with-no-known-parent-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!