无法导入命名空间包“c4d”,但它的任何其他名称都有效

2024-04-23 07:16:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我为自己构建了一个Python库的集合,这些库是在nr命名空间下收集的。现在对于C4D应用程序中的Python插件,我想使用nr.c4d模块,但它不能被导入。奇怪的是,如果我将其重命名为任何其他名称(例如nr.c4dlib),它就可以工作了!在

+ nr.c4d\
  + nr\
    * __init__.py  # < pkgutil.extend_path() in here
    + c4d\
      * __init__.py
      * gui.py
      * ...

这是每个名称空间__init__.py中的代码

^{pr2}$

nr.__path__列表似乎也不错。在

['C:\\maxon\\Cinema 4D R16 Dev\\plugins\\procedural\\python\\nr',
 'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.strex\\nr',
 'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.concurrency\\nr',
 'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.cli\\nr',
 'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.c4d\\nr']

但我知道

Traceback (most recent call last):
  File "'cloudui_frontend.pyp'", line 176, in <module>
ImportError: No module named c4d

可能与环境中有一个名为c4d的内置模块有关。这个错误的来源是什么?如何修复?在


问题似乎来自另一个名为nr.procedural的命名空间模块。它在文件的顶部import c4d。如果该行被注释掉,那么导入nr.c4d就可以了!另外,我可以添加from __future__ import absolute_import,而不是注释掉这一行。在


Tags: 模块pydevinit空间pluginsdevelnr
1条回答
网友
1楼 · 发布于 2024-04-23 07:16:17

在一个时间内创建了条目。这是我在localimport存储库中打开的问题:

Scenario: nr.procedural does import c4d. If nr.c4d does not exist, the module will be added as a None-entry to sys.modules. If then _localimport is used to import nr.c4d, the module will not be found as the module cache says that the module does not exist (by the None-entry).

Solution: _localimport must delete None-entries for namespace packages on __enter__().

fix比较容易。在

我需要将from __future__ import absolute_import添加到nr.的所有nr.子包中。在

相关问题 更多 >