问题
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