question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:6

github_iconTop GitHub Comments

4reactions
JessiiPer-zzcommented, 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 😃

1reaction
csvancecommented, 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​
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection error: Unable to resolve service for type ...
NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I am getting an...
Read more >
Dependency injection guidelines - .NET | Microsoft Learn
Learn various dependency injection guidelines and best practices for .NET application development.
Read more >
Dependency Injection good practices | by Luís Soares | CodeX
I always recommend using constructor injection (in OO) as it makes each component's dependencies evident in tests and implementation.
Read more >
Google Guice Dependency Injection Example Tutorial
Our setup is ready, let's see how to use it with a simple java class. ... The implementation is very easy to understand....
Read more >
Contexts and Dependency Injection - Quarkus
dependencies referenced by quarkus.index-dependency in application.properties , ... In CDI, if you declare a field injection point you need to use @Inject ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found