猿问

如何修复 Python 3.7 导入错误

我有一个非常简单的问题,我的__init__.py文件中有一个 SQLAlchemy 实例。

db = SQLAlchemy()

我试图在我的项目中导入它,但没有成功。

from . import db

感谢您的关注。


慕雪6442864
浏览 88回答 2
2回答

慕勒3428872

看看这个网页: https ://timothybramlett.com/How_to_create_a_Python_Package_with___init__py.html您还可以在答案中提供您的目录结构吗?如果它们位于同一目录中,您应该能够直接在其他文件中使用 db 而无需使用导入

慕侠2389804

我总是讨厌在本地模块中导入,最终不得不做很多次这样的事情:try:  from package.module import thingexcept ModuleNotFoundError:  import os  import sys  here = os.path.realpath(__file__)  root = os.path.split(here)[0]  # add more splits to dig deeper in your structure  sys.path.append(root)  from package.module import thing我希望这会有所帮助。几乎可以肯定不是“正确”的方式来做到这一点。
随时随地看视频慕课网APP

相关分类

Python
我要回答