ImportError:尝试了相对导入,没有已知的父包

问题描述 投票:1回答:1

我正在学习使用python编程,从软件包中的模块导入时遇到问题。我已经测试过注释掉启用了jedi的部分,但是它不起作用。我正在将Visual Studio代码与Python 3.8.2 64位一起使用。

My Project Directory

在我写的products.py文件中:

从.database导入数据库p =数据库(3,2)

以便我可以从database.py模块导入Database类

任何帮助将不胜感激

python pylint
1个回答
0
投票

由于您使用的是Python 3.8版本,因此导入的工作方式略有不同,但是我认为这应该有效:

使用任一:

from database import Database
#Database is the class

或尝试:

import database.Database

最后,这是一个非常安全的做法,并且可能是最佳做法:

from . import Database  
# The '.' (dot) means from within the same directory as this __init__.py module grab the Database class.
© www.soinside.com 2019 - 2024. All rights reserved.