Python:导入变量失败

发布于 2024-12-27 03:11:05 字数 463 浏览 0 评论 0原文

我正在将 Eclipse/PyDev 与 Python2.7 一起用于 Django 中的项目。在我的一个应用程序(例如 app1)中,我在 models.py 中定义了一个变量,并为其分配了列表。我无法将它们导入另一个应用程序(例如 app2)的 models.py 中。假设该项目名为projectName。

projectName/app1/models.py

VARIABLE_NAME = (
    ('a', 'choice1'),
    ('b', 'choice2'),
)

projectName/app2/models.py

from projectName.app1.models import VARIABLE_NAME

我收到来自 Eclipse 的未解决的导入错误:VARIABLE_NAME

有什么想法吗?

I'm using Eclipse/PyDev with Python2.7 for a project in Django. In one of my apps (say app1) I have a variables defined in models.py that have lists assigned to them. I'm failing to import these into models.py of another app (say app2). Say the project is called projectName.

projectName/app1/models.py

VARIABLE_NAME = (
    ('a', 'choice1'),
    ('b', 'choice2'),
)

projectName/app2/models.py

from projectName.app1.models import VARIABLE_NAME

I'm receiving an error from Eclipse of Unresolved Import: VARIABLE_NAME

Any ideas?

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

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

发布评论

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

评论(3

∞梦里开花 2025-01-03 03:11:05

PyDev 的 Eclipse API 也为我做到了这一点。它很难检查 from...import... 样式导入是否实际上会解析为真正的导入。这对我来说看起来很正常。当您运行该程序时,它可能会起作用。如果不是,则很可能是您的错误。

PyDev's Eclipse API does this for me too. It has troubles checking if from...import... style imports will actually resolve to a real import. This looks pretty normal to me. When you run the program, it will likely work. If not, it's more than likely an error on your part.

终止放荡 2025-01-03 03:11:05

我认为以下是发生的情况:

1) Eclipse 看到您尝试导入,但 projectName 不是 sys.path 中的包(除非您显式添加它),所以它对您很好并向您抱怨。

2)问题是projectName不应该是一个包;至少现在还没有。根据 https://docs.djangoproject.com/en/dev/ref/ django-admin/ ,它说“此外,manage.py...将您的项目的包放在 sys.path 上。”

因此,我会忽略“错误”,即使 Eclipse 在技术上是正确的,您无法进行该导入。 Django 设置的要点是,当您执行“python manage.py runserver”时,projectName 实际上将可用,因此一切都很好。 =)

I think the following is what's going on:

1) Eclipse sees you trying to import, but projectName is not a package (unless you added it explicitly) in sys.path, so it is being nice and complaining to you.

2) the problem is that projectName is not supposed to be a package; at least not yet. According to https://docs.djangoproject.com/en/dev/ref/django-admin/ , it says that "In addition, manage.py... puts your project’s package on sys.path."

Thus I would ignore the "error," even though Eclipse is technically correct that you cannot make that import. The point of the Django setup is when you do "python manage.py runserver" the projectName will actually be available, so everything is good. =)

梦里梦着梦中梦 2025-01-03 03:11:05

找到了解决方案。正如 yanxhang 指出的那样,这是一个 PYTHONPATH 问题。在 Eclipse 的首选项中,在 PyDev > 下解释器,在系统 PYTHONPATH 的底部窗口中,确保添加作为项目名称的父级的文件夹,然后单击“应用”。这允许 projectName 解析其自身及其所有子项。

Found a solution. This is a PYTHONPATH problem, as yanxhang pointed out. In Eclipse's Preferences, under PyDev > Interpreter, in the bottom window for System PYTHONPATH, make sure to add the folder that is the parent of your projectName and click apply. This allows for the projectName to resolve itself and all of its children.

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