ImportError:在python 2.7中没有名为<modulename>的模块

时间:2019-12-05 11:44:25

标签: python

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

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

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

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

  

回溯(最近通话最近):     在第1行的文件“ ComponentStatus.py”中       从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))

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

2 个答案:

答案 0 :(得分:0)

它将是:

from main.BitesizeDecorator import BitesizeDecorator

答案 1 :(得分:0)

您可以将文件BitesizeClusterInfo.py的位置更改为主文件夹,以使您的工作更加顺畅。

或使用

from main.BitesizeDecorator import BitesizeDecorator

,如果您的主文件夹是项目源:

from main.BitesizeDecorator import BitesizeDecorator