2

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

asked Aug 26, 2021 at 12:58

1 Answer 1

2

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__)]).

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Aug 27, 2021 at 10:59
Sign up to request clarification or add additional context in comments.

3 Comments

I had already tried that. I get the same ValueError: path must be None or list of paths to look for modules in
@Foad I've edited the answer. Kindly try pkgutil.iter_modules([os.path.dirname(NXOpen.__file__)])
strangely enough, adding the square brackets [...] helped!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.