I just discovered that while doing check of the functions available in os.path using dir(os.path) is not the same as shown by dir(__import__("os.path", globals={}, locals={}, fromlist=[], level=-1)),
in the later case it is actually showing all the modules of OS while I just want to look inside for the modules that are inside of path of os.

I have used example of os.path, actually in the example I am working on has module and method input by user..

please explain why is there a difference how should I get the same result while using dir(__import__("os.path")) just as in dir(os.path)??

Recommended Answers

All 2 Replies

When fromlist is empty, __import__('A.B.C') returns module A, although it loads A, A.B and A.B.C and inserts them in sys.modules. Try

mod = __import__("A.B.C", fromlist=["__name__"])

Hmm, in fact mod = importlib.import_module("A.B.C") is officially better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.