Listing all the modules/subpackages in the NXOpen package

1.2k Views Asked by At

I am trying to follow this post, to list all the modules in the NX Open package:

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()

but I get the below error message:


pkgutil.py, line 123, in iter_modules raise ValueError("path must be None or list of path to look for")

What is the problem and how can I resolve it?

P.S. 1. I checked the NXOpen.__path__ attribute. It is a string type property with a value of NXOpenPackagePath!

P.S. 2. I asked a follow up question, here on the SIEMENS PLM forum.

P.S.3. I also tried the

import os
from pathlib import Path

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

from here and here, but now I get the error:

TypeError: 'WindowsPath' object is not iterable

1

There are 1 best solutions below

3
On BEST ANSWER

Judging by the source code of iter_modules, it expects list argument, while you pass a Path object.

Please, try just pkgutil.iter_modules([os.path.dirname(NXOpen.__file__)]).