A Python namespace package can be spread over many directories, and zip files or custom importers. What's the correct way to iterate over all the importable submodules of a namespace package?
Asked
Active
Viewed 2,981 times
7
-
2What are you trying to accomplish? What have you tried? – Daenyth May 22 '12 at 03:10
-
1You can use the pkgutil module. http://stackoverflow.com/questions/3365740/how-to-import-all-submodules – hostingutilities.com May 22 '12 at 03:23
-
I am interested in using it for plugin enumeration. – joeforker May 22 '12 at 14:12
1 Answers
0
Please read import confusion.
It very clearly distinguishes all the different ways you can import packages and its sub modules and in the process answers your question. When you need a certain submodule from a package, it’s often much more convenient to write from io.drivers import zip than import io.drivers.zip, since the former lets you refer to the module simply as zip instead of its full name.
from modname import *, this provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.
Ben
- 50,172
- 36
- 122
- 141
The Recruit
- 765
- 3
- 8
- 17
-
I'm specifically not asking about "import *", when you "import *" from a namespace package nothing happens. Instead, I want to iterate through everything that I could import. – joeforker May 22 '12 at 10:25
-
Using `*` imports is terrible practice. While it's valid python, it's almost always a bad idea outside of a few narrow cases. – Daenyth May 22 '12 at 14:01
-
Yes I do agree, as it reduces the time efficiency, but in situation where one does not know the particular module to be used , importing the entire package becomes the saviour. – The Recruit May 23 '12 at 06:41