【问题标题】:Listing all the modules/subpackages in the NX Open package列出 NX Open 包中的所有模块/子包
【发布时间】:2021-10-26 13:00:21
【问题描述】:

我正在尝试关注this post,列出 NX Open 包中的所有模块:

import NXOpen
import pkgutil

the_string = ""

package = NXOpen
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
    the_string = the_string + modname + "\n"


lw = NXOpen.Session.GetSession().ListingWindow
lw.Open()
lw.WriteFullline(the_string)
lw.Close()

但我收到以下错误消息:

pkgutil.py,第 123 行,在 iter_modules 中引发 ValueError("path must be None or list of path to find")

问题是什么,我该如何解决?

附注1. 我检查了NXOpen.__path__ 属性。它是一个字符串类型的属性,值为NXOpenPackagePath!

附注2.我问了一个后续问题,here on the SIEMENS PLM forum

P.S.3.我也试过了

import os
from pathlib import Path

for importer, modname, ispkg in pkgutil.iter_modules(Path(os.path.dirname(NXOpen.__file__))):

来自herehere,但现在我得到了错误:

TypeError: 'WindowsPath' 对象不可迭代

【问题讨论】:

    标签: python nxopen


    【解决方案1】:

    根据iter_modulessource code 判断,它需要列表参数,而您传递Path object

    请尝试pkgutil.iter_modules([os.path.dirname(NXOpen.__file__)])

    【讨论】:

    • 我已经试过了。我得到相同的ValueError: path must be None or list of paths to look for modules in
    • @Foad 我已经编辑了答案。请尝试pkgutil.iter_modules([os.path.dirname(NXOpen.__file__)])
    • 奇怪的是,添加方括号[...] 有帮助!
    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    相关资源
    最近更新 更多