发布于 2015-08-30 07:51:37 | 364 次阅读 | 评论: 0 | 来源: 网络整理

问题

You have the name of a module that you would like to import, but it’s being held in a string. You would like to invoke the import command on the string.


解决方案

Use the importlib.import_module() function to manually import a module or part of a package where the name is given as a string. For example:

>>> import importlib
>>> math = importlib.import_module('math')
>>> math.sin(2)
0.9092974268256817
>>> mod = importlib.import_module('urllib.request')
>>> u = mod.urlopen('http://www.python.org')
>>>

import_module simply performs the same steps as import, but returns the resulting module object back to you as a result. You just need to store it in a variable and use it like a normal module afterward.

If you are working with packages, import_module() can also be used to perform relative imports. However, you need to give it an extra argument. For example:

import importlib
# Same as 'from . import b'
b = importlib.import_module('.b', __package__)

讨论

The problem of manually importing modules with import_module() most commonly arises when writing code that manipulates or wraps around modules in some way. For example, perhaps you’re implementing a customized importing mechanism of some kind where you need to load a module by name and perform patches to the loaded code.

In older code, you will sometimes see the built-in __import__() function used to perform imports. Although this works, importlib.import_module() is usually easier to use.

See Recipe 10.11 for an advanced example of customizing the import process.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务