Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Unable to run the basic dependency injection example mentioned in documentation. #41

Open
bnriiitb opened this issue Jul 18, 2019 · 6 comments

Comments

@bnriiitb
Copy link

bnriiitb commented Jul 18, 2019

Issue Description:

Unable to run the example mentioned in the documentation (https://github.com/google/pinject) of pinject. Getting ModuleNotFoundError: No module named '_gdbm'.

I've installed python-gdbm through conda but still, I see the ModuleNotFoundError. Could you please help me to understand what could be the issue?

conda install -c anaconda python-gdbm

Code:

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)

Error Log Trace:

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'
@avilum
Copy link

avilum commented Dec 11, 2019

Happened to me as well.. anyone?

@arisliang
Copy link

arisliang commented Apr 25, 2020

me too.

ubuntu: 18.04
python: 3.7
pinject: 0.14.1

@csvance
Copy link

csvance commented Jul 28, 2020

Same.

  • Ubuntu 18.04
  • Python 3.7.7 (Anaconda, all dependencies installed through conda not pip)
  • six 1.15.0

Here is a quick hack that seems to get the job done in pinject/finding.py

def find_classes(modules, classes):
    if classes is not None:
        all_classes = set(classes)
    else:
        all_classes = set()
    for module in _get_explicit_or_default_modules(modules):
        # TODO(kurts): how is a module getting to be None??
        if module is not None and str(module).find('six.moves') == -1:
            all_classes |= _find_classes_in_module(module)
    return all_classes

@csvance
Copy link

csvance commented Oct 30, 2020

I have been using this hack for testing a new version of a backend solution which uses pinject for the past several months. So far we have had no issues with it but I am hesitant to send something like this into production. I may have time to properly address this in December. If the algorithm has a bug I will try and fix it. If assumptions made by the algorithm are violated by six I will see if I can accommodate them. Otherwise a module blacklist might be the only solution which could be done in a reasonable amount of time.

@JessiiPer-zz
Copy link

JessiiPer-zz commented Nov 6, 2020

Hey guys. I was with the same problems. When you use obj_graph = pinject.new_object_graph() by default looks in all imported modules, but you may occasionally want to restrict the classes for which new_object_graph() creates implicit bindings. If so, new_object_graph() has two args for this purpose.

So, you can use:
obj_graph = pinject.new_object_graph(modules=None, classes=[SomeClass])

This resolved my problem :)

@kootoopas
Copy link

kootoopas commented Jan 7, 2021

Either the error message should be changed to something more precise, since the issue may be caused by not providing the modules arg, or the factory function should default to the modules arg being None-type so that it doesn't occur.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants