سؤال

I am trying to import a uberModule and retrieve the class "test" but getattr can't find it, why ? Thanks.

AttributeError: 'module' object has no attribute 'test'

Here's my code, main.py:

from importlib import import_module
module = import_module("modules.uberModule")
cls = getattr(module, 'test')

uberModule.py :

class test():
    def __init__(self):
       print "such wow"

Directory structure :

.
├── modules
│   ├── __init__.py
│   └── uberModule
│       ├── __init__.py
│       └── uberModule.py
├── __init__.py
├── main.py
هل كانت مفيدة؟

المحلول

That should be:

module = import_module("modules.uberModule.uberModule")

unless uberModule/__init__.py imports the contents of uberModule (I guess not)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top