我怎样才能得到 'urlpatterns = __import__()'像普通导入语句一样工作?

发布于 2024-09-16 18:00:24 字数 342 浏览 7 评论 0原文

我正在尝试创建一个可与其他项目插入的导入语句。 该语句位于 urls.py

所以这有效:

from forum.urls import urlpatterns
# Base Class:   <type 'list'>

但这不起作用:

from settings import ROOT_URLCONF as project_urls
urlpatterns = __import__(project_urls)
# Base Class:   <type 'module'>

如何让后者工作?

I'm trying to create an import statement that's pluggable with other projects.
This statement is located in urls.py

So this works:

from forum.urls import urlpatterns
# Base Class:   <type 'list'>

But this doesn't work:

from settings import ROOT_URLCONF as project_urls
urlpatterns = __import__(project_urls)
# Base Class:   <type 'module'>

How can I get the latter to work?

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

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

发布评论

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

评论(2

握住你手 2024-09-23 18:00:24

那么您想要拥有与早期导入相关的导入语句吗?

这绝对是我曾经尝试过的一件事。我有一些很长的导入语句,它们有一个共同的根源,所以我试图将其分解出来。我无法让它与直接导入语句一起工作,但也许我没有足够努力。

请记住,默认情况下,导入语句行为将创建一个模块对象。然后它会将其绑定到 sys.modules 中,然后使用 import 语句中的名称将其绑定到当前模块的命名空间中。请参阅http://docs.python.org/tutorial/modules.html

模块对象有一个命名空间。如果模块不是包,则它的命名空间来自评估模块的 .py 文件的内容。但是,如果它是一个包,那么命名空间来自包中的 __init__.py 模块。包中的其他模块不会自动导入,并且在包的命名空间中不可用。您必须单独导入它们。

from...import 语句会将模块加载到 sys.modules 中。然后它会将对象从您在导入中引用的模块中拉出。最后,它使用 import 语句中的名称将该对象绑定到当前模块的名称空间中。基本上,您是将绑定从一个名称空间复制到另一个名称空间。老实说,我发现当你稍后使用它时,它通常会混淆名称的来源(所以我不做太多)。

要点:

使用 __import__ 是绕过 import 语句限制的一种方法。请参阅Python 文档。但是,如果您使用 from..import 语句,请不要尝试在 __import__ 中重用生成的名称,除非它指向模块对象(它可能不是)。导入仅需要以点分隔的模块名称序列。

另外,请记住,仅进行显式导入可能是指示对象来自何处的更简洁的方法。

So you want to have import statements that are relative to earlier imports?

Definitely something I tried at one point. I had some very long import statements that had a common root, so I tried to factor it out. I could not get it to work with straight import statements, but maybe I didn't try hard enough.

Keep in mind that the import statement behavior by default is going to create a module object. It will then bind it into sys.modules, and then bind it in your current module's namespace with the name from the import statement. See http://docs.python.org/tutorial/modules.html.

A module object has a namespace. If a module is not a package, it's namespace comes from evaluating the contents of the .py file of the module. However, if it is a package then the namespace comes from the __init__.py module in the package. The other modules in the package are not imported automatically and are not available in the package's namespace. You have to import them separately.

The from...import statement will load the module into sys.modules. Then it will pull the object out of that module to which you referred in the import. Finally it binds that object into your current module's namespace with the name from the import statement. Basically you are copying a binding from one namespace to another. To be honest I find that it usually obfuscates the source of the name when you use it later (so I don't do it much).

To the point:

Your use of __import__ is one way around the limits of the import statement. See the python documentation. However, if you use a from..import statement don't try to reuse the resulting name in __import__ unless that is pointing to a module object (which it probably isn't). Imports need a dot-delimited sequence of module names only.

As well, keep in mind that just putting the explicit import may be a cleaner way to indicate where an object came from.

赠我空喜 2024-09-23 18:00:24
urlpatterns = __import__(project_urls).whateversubmodule.urlpatterns
urlpatterns = __import__(project_urls).whateversubmodule.urlpatterns
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文