I’m working on a documentation (personal) for nested matplotlib (MPL) library, which differs from MPL own provided, by interested submodule packages. I’m writing Python script which I hope will automate document generation from future MPL releases.
I selected interested submodules/packages and want to list their main classes from which I’ll generate list and process it with pydoc
Problem is that I can’t find a way to instruct Python to load submodule from string. Here is example of what I tried:
import matplotlib.text as text
x = dir(text)
.
i = __import__('matplotlib.text')
y = dir(i)
.
j = __import__('matplotlib')
z = dir(j)
And here is 3 way comparison of above lists through pprint:
I don’t understand what’s loaded in y object – it’s base matplotlib plus something else, but it lack information that I wanted and that is main classes from matplotlib.text package. It’s top blue coloured part on screenshot (x list)
Please don’t suggest Sphinx as different approach.
回答 0
该__import__功能可能有点难以理解。
如果你改变
i = __import__('matplotlib.text')
至
i = __import__('matplotlib.text', fromlist=[''])
然后i将参考matplotlib.text。
在Python 2.7和Python 3.1或更高版本中,可以使用importlib:
import importlib
i = importlib.import_module("matplotlib.text")
pipmodules =['pycurl','ansible','bad_module_no_beer']for module in pipmodules:try:# because we want to import using a variable, do it this way
module_obj = __import__(module)# create a global object containging our module
globals()[module]= module_obj
exceptImportError:
sys.stderr.write("ERROR: missing python module: "+ module +"\n")
sys.exit(1)
spent some time trying to import modules from a list, and this is the thread that got me most of the way there – but I didnt grasp the use of ___import____ –
so here’s how to import a module from a string, and get the same behavior as just import. And try/except the error case, too. :)
pipmodules = ['pycurl', 'ansible', 'bad_module_no_beer']
for module in pipmodules:
try:
# because we want to import using a variable, do it this way
module_obj = __import__(module)
# create a global object containging our module
globals()[module] = module_obj
except ImportError:
sys.stderr.write("ERROR: missing python module: " + module + "\n")
sys.exit(1)
and yes, for python 2.7> you have other options – but for 2.6<, this works.
Finally I can call all the functions inside the new Instance:
myInstance.aFunction()
The only specificity here is to customize the params list (param1, param2, param3) of your instance.
回答 4
除了使用importlib一个,还可以使用exec方法从字符串变量导入模块。
在这里,我展示了使用combinations方法从itertools包中导入方法的示例exec:
MODULES =[['itertools','combinations'],]for ITEM in MODULES:
import_str ="from {0} import {1}".format(ITEM[0],', '.join(str(i)for i in ITEM[1:]))exec(import_str)
ar = list(combinations([1,2,3,4],2))for elements in ar:print(elements)
Apart from using the importlib one can also use exec method to import a module from a string variable.
Here I am showing an example of importing the combinations method from itertools package using the exec method:
MODULES = [
['itertools','combinations'],
]
for ITEM in MODULES:
import_str = "from {0} import {1}".format(ITEM[0],', '.join(str(i) for i in ITEM[1:]))
exec(import_str)
ar = list(combinations([1, 2, 3, 4], 2))
for elements in ar:
print(elements)