Django 从另一个文件夹中的不同模型导入
我的管理应用程序中有两个文件夹 - 模块和测试。每个文件夹都包含自己的模型。我正在尝试在两个文件夹之间导入。
从测试中的模块导入: from exam.admin.modules.models import subject
它工作正常,但 Pydev Eclipse 显示了一个未解决的导入错误。有什么理由吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,只要您的程序运行,您就可以安全地忽略 PyDev/Eclipse 的警告。 PyDev 似乎喜欢项目内的相对导入。您可以在导入路径中省略项目名称,但有一种更清晰的方法可以使用点显示层次结构。 “上一级,进入模块目录,然后从 models.py 文件导入主题。”
from ..modules.models import subject
有关 Python 导入标准(绝对与相对建议)的更多信息,请参见 PEP 328
As others have said, as long as your program runs then you can safely ignore PyDev/Eclipse's warning. PyDev seems to like relative imports within a project. You could leave off the project name from the import path, but there's a clearer way to show the hierarchy using dots. "Go up one level, into the modules directory, and import Subject from the models.py file."
from ..modules.models import Subject
More on Python importing standards (absolute versus relative recommendations) in PEP 328
您可以尝试配置源文件夹并检查是否可以解决问题。
右键单击项目,单击属性。
打开 PyDev - PYTHONPATH 选项卡。
检查定义的源文件夹。
这些源文件夹是您的“根”源文件夹。
未找到的类的目录可能没有源文件夹。
You may try to configure Source Folders and check if this can solve the problem.
Right click on the Project, click on Properties.
Open the PyDev - PYTHONPATH tab.
Check the Source Folders defined.
These Source Folders are your "root" source folders.
Probably there is no source folder for the directory of the not found classes.
如果您的应用程序正常工作,我不会担心 PyDev 的导入错误。 我确信 PyDev/Eclipse 中存在错误
I would not worry about PyDev's import error if your app works. I am sure there are bugs in PyDev/Eclipse