(import_with_attr_access)
| 492 | |
| 493 | |
| 494 | def import_attr_from_module(import_with_attr_access): |
| 495 | if "." not in import_with_attr_access: |
| 496 | # We need at least one '.' (we don't support just the module import, we need the attribute access too). |
| 497 | raise ImportError("Unable to import module with attr access: %s" % (import_with_attr_access,)) |
| 498 | |
| 499 | module_name, attr_name = import_with_attr_access.rsplit(".", 1) |
| 500 | |
| 501 | while True: |
| 502 | try: |
| 503 | mod = import_module(module_name) |
| 504 | except ImportError: |
| 505 | if "." not in module_name: |
| 506 | raise ImportError("Unable to import module with attr access: %s" % (import_with_attr_access,)) |
| 507 | |
| 508 | module_name, new_attr_part = module_name.rsplit(".", 1) |
| 509 | attr_name = new_attr_part + "." + attr_name |
| 510 | else: |
| 511 | # Ok, we got the base module, now, get the attribute we need. |
| 512 | try: |
| 513 | for attr in attr_name.split("."): |
| 514 | mod = getattr(mod, attr) |
| 515 | return mod |
| 516 | except: |
| 517 | raise ImportError("Unable to import module with attr access: %s" % (import_with_attr_access,)) |
no outgoing calls