ImportError:在Python2.7中没有名为<modulename>的模块

2024-09-29 19:30:45 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图在另一个目录中导入另一个python类时,出现了这个错误。你知道吗

我的文件夹结构如下所示:

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

我试图从BitesizeClusterInfo.py导入BitesizeDecorator.py,但出现以下错误:

Traceback (most recent call last): File "ComponentStatus.py", line 1, in from BitesizeDecorator import BitesizeDecorator ImportError: No module named 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))

有人能帮我解决这个问题吗?你知道吗


Tags: frompyimportselfexecuteinitmaindef
2条回答

可以将文件BitesizeClusterInfo.py的位置更改为主文件夹,以使行正常工作。你知道吗

或使用

from main.BitesizeDecorator import BitesizeDecorator

如果主文件夹是项目源:

from main.BitesizeDecorator import BitesizeDecorator

它将是:

from main.BitesizeDecorator import BitesizeDecorator

相关问题 更多 >

    热门问题