给定完整模块路径动态导入可调用对象?
>>> import_path('os.path.join')
<function join at 0x22d4050>
编写 import_path
(在 Python 2.6 及更高版本中)的最简单方法是什么?假设最后组件始终是模块/包中的可调用组件。
>>> import_path('os.path.join')
<function join at 0x22d4050>
What is the simplest way to write import_path
(in Python 2.6 and above)? Assume that the last component is always a callable in a module/package.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎就是您想要的:
要使其与 Python 2.5 及更早版本一起使用,其中
__import__
不接受关键字参数,您将需要使用:This seems to be what you want:
To make it work with Python 2.5 and earlier, where
__import__
doesn't take keyword arguments, you will need to use:显然有以下工作:
Apparently the following works:
尝试一下有效
至少
现在
Try
Works at least for
and now