首页 > 解决方案 > ImportError:没有命名的模块在蟒蛇 2.7

问题描述

当我尝试在不同目录中导入另一个 python 类时出现此错误。

这就是我的文件夹结构的样子:

main
    /prerequisites
         - __init__.py
         - BitesizeClusterInfo.py
         - ComponentStatus.py
__init__.py
BitesizeDecorator.py
BitesizeImp.py
BitesizeInterface.py
constants.py
execute.py
main.py

我正在尝试从中导入BitesizeDecorator.pyBitesizeClusterInfo.py但出现此错误:

Traceback(最近一次调用最后一次):文件“ComponentStatus.py”,第 1 行,从 BitesizeDecorator 导入 BitesizeDecorator ImportError:没有名为 BitesizeDecorator 的模块

这就是我的代码片段的BitesizeClusterInfo.py样子:

import os

from BitesizeDecorator import BitesizeDecorator
from execute import Execute

class BitesizeClusterInfo(BitesizeDecorator):
    def __init__(self, bitesize):
        super(BitesizeClusterInfo, self).__init__(bitesize)

    def test(self):
        super(BitesizeClusterInfo, self).test()

        # add command below
        print("\n[1] - Checking cluster info...\n")

        # grep the output for ease of reading
        cmd = "kubectl cluster-info | grep -E 'master|DNS'"
        print(Execute.check_if_exists(cmd))

有人可以帮我解决这个问题吗?

标签: python

解决方案


这将是:

from main.BitesizeDecorator import BitesizeDecorator

推荐阅读