python - What do I need to do to dynamically import a module?
问题描述
Can someone tell me why this doesn't work?
m = importlib.import_module('operator')
eval('operator.add') # returns NameError
I know I can use m.add
but I can't do that as I am reading the input to eval
from a file. Is there a way I can get m
to be loaded into the current runtime?
解决方案
You simply need to assign the result of import_module()
to a variable named operator
instead of m
.
operator = importlib.import_module('operator')
Is equivalent to:
import operator
推荐阅读
- vba - 将表单字段设置为自动填充文本、日期和组合框的先前记录条目
- python - Android Kivy Python上的TextInput键盘问题
- swift - 根据代码库 Swift 中 NSLocalizedString 的出现生成 Localizable.strings 文件
- javascript - 为什么 node-lame 不能正确编码(nodeJS 库)?
- linux - 我无法从 PhpStorm 保存到 WSL2
- c++ - 如何跟踪 C/C++ 程序的调用函数和执行语句?
- crm - Zoho CRM 移动应用程序上的通话录音和通话记录
- python - / 'NoneType' 对象的 AttributeError 没有属性 '_get_qnames_to_try'
- laravel - Laravel 路由中间件除了路由配置还能用在哪里?
- javascript - 使用地图功能上的 onclick 切换打开和关闭