简体   繁体   English

导入错误:没有命名模块<modulename>在 python 2.7 中

[英]ImportError: No module named <modulename> in python 2.7

I am getting this error when I try to import a another python class in a different directory.当我尝试在不同目录中导入另一个 python 类时出现此错误。

this is how my folder structure looks like:这是我的文件夹结构的样子:

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

I am trying to import BitesizeDecorator.py from BitesizeClusterInfo.py and I get this error:我正在尝试从BitesizeClusterInfo.py导入BitesizeDecorator.py ,但出现此错误:

Traceback (most recent call last): File "ComponentStatus.py", line 1, in from BitesizeDecorator import BitesizeDecorator ImportError: No module named BitesizeDecorator回溯(最近一次调用):文件“ComponentStatus.py”,第 1 行,来自 BitesizeDecorator import BitesizeDecorator ImportError: No module named BitesizeDecorator

And this is how my code snippet for BitesizeClusterInfo.py looks like:这就是我的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))

Can someone please help me solve this?有人可以帮我解决这个问题吗?

这将是:

from main.BitesizeDecorator import BitesizeDecorator

You can change the position of the file BitesizeClusterInfo.py to the main folder to make you line work.您可以将文件BitesizeClusterInfo.py的位置更改为主文件夹,以使您的线条工作。

Or use或使用

from main.BitesizeDecorator import BitesizeDecorator

and if your main folder is the project source :如果您的主文件夹是项目源:

from main.BitesizeDecorator import BitesizeDecorator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM