无法运行Pinject文档中提到的基本依赖项注入示例

时间:2019-07-18 06:33:44

标签: python-3.x dependency-injection

问题说明:

无法运行pinject的文档(https://github.com/google/pinject)中提到的示例。正在获取ModuleNotFoundError: No module named '_gdbm'

我已经通过conda安装了python-gdbm,但仍然看到ModuleNotFoundError。谁能帮我了解可能是什么问题?

conda install -c anaconda python-gdbm

代码

import pinject

class OuterClass(object):
    def __init__(self, inner_class):
         self.inner_class = inner_class

class InnerClass(object):
    def __init__(self):
         self.forty_two = 42

obj_graph = pinject.new_object_graph()
outer_class = obj_graph.provide(OuterClass)
print(outer_class.inner_class.forty_two)

错误日志跟踪:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-6f1689830d13> in <module>()
      9          self.forty_two = 42
     10 
---> 11 obj_graph = pinject.new_object_graph()
     12 outer_class = obj_graph.provide(OuterClass)
     13 # print(outer_class.inner_class.forty_two)

~/anaconda3/lib/python3.6/site-packages/pinject/object_graph.py in new_object_graph(modules, classes, binding_specs, only_use_explicit_bindings, allow_injecting_none, configure_method_name, dependencies_method_name, get_arg_names_from_class_name, get_arg_names_from_provider_fn_name, id_to_scope, is_scope_usable_from_scope, use_short_stack_traces)
     98         known_scope_ids = id_to_scope.keys()
     99 
--> 100         found_classes = finding.find_classes(modules, classes)
    101         if only_use_explicit_bindings:
    102             implicit_class_bindings = []

~/anaconda3/lib/python3.6/site-packages/pinject/finding.py in find_classes(modules, classes)
     30         # TODO(kurts): how is a module getting to be None??
     31         if module is not None:
---> 32             all_classes |= _find_classes_in_module(module)
     33     return all_classes
     34 

~/anaconda3/lib/python3.6/site-packages/pinject/finding.py in _find_classes_in_module(module)
     44 def _find_classes_in_module(module):
     45     classes = set()
---> 46     for member_name, member in inspect.getmembers(module):
     47         if inspect.isclass(member) and not member_name == '__class__':
     48             classes.add(member)

~/anaconda3/lib/python3.6/inspect.py in getmembers(object, predicate)
    340         # looking in the __dict__.
    341         try:
--> 342             value = getattr(object, key)
    343             # handle the duplicate key
    344             if key in processed:

~/anaconda3/lib/python3.6/site-packages/six.py in __get__(self, obj, tp)
     90 
     91     def __get__(self, obj, tp):
---> 92         result = self._resolve()
     93         setattr(obj, self.name, result)  # Invokes __set__.
     94         try:

~/anaconda3/lib/python3.6/site-packages/six.py in _resolve(self)
    113 
    114     def _resolve(self):
--> 115         return _import_module(self.mod)
    116 
    117     def __getattr__(self, attr):

~/anaconda3/lib/python3.6/site-packages/six.py in _import_module(name)
     80 def _import_module(name):
     81     """Import module, returning the module after the last dot."""
---> 82     __import__(name)
     83     return sys.modules[name]
     84 

~/anaconda3/lib/python3.6/dbm/gnu.py in <module>()
      1 """Provide the _gdbm module as a dbm submodule."""
      2 
----> 3 from _gdbm import *

ModuleNotFoundError: No module named '_gdbm'

0 个答案:

没有答案