Django 从另一个文件夹中的不同模型导入

发布于 2024-11-04 13:11:00 字数 191 浏览 5 评论 0 原文

我的管理应用程序中有两个文件夹 - 模块和测试。每个文件夹都包含自己的模型。我正在尝试在两个文件夹之间导入。

从测试中的模块导入: from exam.admin.modules.models import subject

它工作正常,但 Pydev Eclipse 显示了一个未解决的导入错误。有什么理由吗?

I have two folders - modules and test - within my admin app. Each folder contains its own models. I'm trying to import between the two folders.

Importing from modules in test: from exam.admin.modules.models import Subject

It works fine, but Pydev Eclipse shows me an unresolved import error. Any reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉殇 2024-11-11 13:11:00

正如其他人所说,只要您的程序运行,您就可以安全地忽略 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

混吃等死 2024-11-11 13:11:00

您可以尝试配置源文件夹并检查是否可以解决问题。
右键单击项目,单击属性。
打开 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.

请叫√我孤独 2024-11-11 13:11:00

如果您的应用程序正常工作,我不会担心 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文