文件夹中的文件夹中的__getattr__和__import__不起作用

时间:2019-07-24 08:12:23

标签: python python-3.x

我不知道发生了什么变化,但是以下代码以前在我的项目中运行过,现在却没有:

# From Imports
from loguru import logger
from mm import colorize
from sys import exit
from typing import Callable, Any

ls: Callable[..., Any] = logger.success
li: Callable[..., Any] = logger.info
le: Callable[..., Any] = logger.error


# Results in a recursive import if in "mm", as the type_classes files import "mm" as well
def run_backup_type_functions(
    d, func, use_base, *args, **kwargs
) -> None:
    try:
        #############################################
        backup_type_folder: type = getattr(
            __import__("type_classes"), d.backup_type
        )
        #############################################
        backup_type_file: type = getattr(
            backup_type_folder, d.backup_type
        )
        argument_class: type = getattr(
            backup_type_file,
            "base" if use_base else d.argument,
            None,
        )(d, *args, **kwargs)
        getattr(argument_class, func)()
    except TypeError:
        if use_base:
            le(colorize("red", "Sorry; no base exists!"))
        else:
            le(
                colorize("red", 'Sorry; no argument "')
                + colorize("pink", a)
                + colorize("red", '"!')
            )
        exit(1)
    except Exception as e:
        le(
            colorize(
                "red",
                "Sorry; something happened! This was the error:\n\n",
            )
            + colorize("pink", e)
            + "\n"
        )
        exit(1)


def get_backup_type_attr(d, attr, use_base) -> Any:
    try:
        backup_type_folder: type = getattr(
            __import__("type_classes"), d.backup_type
        )
        backup_type_file: type = getattr(
            backup_type_folder, d.backup_type
        )
        argument_class: type = getattr(
            backup_type_file,
            "base" if use_base else d.argument,
            None,
        )(d)
        getattr(argument_class, func)()
    except TypeError:
        if use_base:
            le(colorize("red", "Sorry; no base exists!"))
        else:
            le(
                colorize("red", 'Sorry; no argument "')
                + colorize("pink", a)
                + colorize("red", '"!')
            )
        exit(1)
    except Exception as e:
        le(
            colorize(
                "red",
                "Sorry; something happened! This was the error:\n\n",
            )
            + colorize("pink", e)
            + "\n"
        )
        exit(1)

具体来说,当使用AttributeError: module 'type_classes' has no attribute 'borg'从父目录文件run_backup_type_function动态导入type_classes子文件夹作为模块时,错误是mm2.py中的d.backup_type保存要导入的子文件夹的变量;这在代码的散列区域中显示。我的源目录树如下所示:

.
├── create.py
├── docker_cmd.py
├── __init__.py
├── LICENSE
├── meltan.py
├── mm2.py
├── mm.py
├── pull.py
├── pyproject.toml
├── README.md
├── regular_cmd.py
├── start.sh
├── test2.py
├── test2.sh
├── test.py
├── tests
├── test.sh
├── tmp
├── total_cmd.py
└── type_classes
    ├── borg
    │   ├── borg.py
    │   ├── Dockerfile
    │   ├── __init__.py
    │   └── __pycache__
    │       └── borg.cpython-37.pyc
    ├── git
    │   └── git.py
    ├── __init__.py
    ├── mercurial
    │   └── hg.py
    └── __pycache__
        ├── borg.pypy3-71.pyc
        ├── __init__.cpython-37.pyc
        ├── __init__.cpython-38.pyc
        └── __init__.pypy3-71.pyc


5 directories, 10 files

type_classes/__init__.py文件中没有任何内容。我也尝试过将from borg import borg放在type_classes/__init__.py文件中,但是该文件夹未注册,我认为这也是原始问题的原因。另外,我尝试将文件从borg内部移至其父目录(即type_classes),但文件名相同。AttributeError。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

回答了我自己的问题,我切换到将模块路径附加到python路径,然后从那里导入子模块:

sys_path.append(
    fullpath(
        os_path.dirname(__file__),
        "type_classes",
        d.backup_type,
    )
)
argument_class: type = getattr(
    __import__(f"mm_{d.backup_type}"),
    "base" if use_base else d.argument,
    None,
)(d, *args, **kwargs)
getattr(argument_class, func)()