代码之家  ›  专栏  ›  技术社区  ›  hydos06

导入错误:ModuleNotFoundError:“block\u model”不是包

  •  0
  • hydos06  · 技术社区  · 7 年前

    我正在尝试使用python open\u gl并创建一个创建对象类。Im获取错误ModuleNotFoundError:没有名为“block\u model”的模块。py';'block\u model不是一个包,我的代码给出了我无法修复的错误: `

    class Create:
        def __init__(self, cube_x, cube_y, cube_z, file):
            glBegin(GL_LINES)
            model = importlib.import_module(file)
            model.verticies()
            model.edges()
            verticies = model.verticies
            edges = model.edges
            for edge in edges:
                for vertex in edge:
                    glVertex3fv(verticies[vertex])
            glEnd()
        c1_x = 0
        c1_y = 0
        c1_z = 0
        print(camera)
    cube1 = Create(c1_x, c1_y, c1_z, file="block_model.py")
    

    ` 这就是block\u模型。py看起来像:

    verticies = (
        (1, -1, -1),
        (1, 1, -1),
        (-1, 1, -1),
        (-1, -1, -1),
        (1, -1, 1),
        (1, 1, 1),
        (-1, -1, 1),
        (-1, 1, 1)
    )
    edges = (
        (0, 1),
        (0, 3),
        (0, 4),
        (2, 1),
        (2, 3),
        (2, 7),
        (6, 3),
        (6, 4),
        (6, 7),
        (5, 1),
        (5, 4),
        (5, 7)
    )
    
    
    def verticies():
        # global verticies
        print("Returning verticies")
    
        return verticies
    
    
    def edges():
        # global verticies
        print("Returning edges")
    
        return edges
    

    谢谢:) **编辑:*我有一个新问题,它现在说:对于边中的边:TypeError:“function”对象不可iterable。有人对此有办法吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Arjun Kay    7 年前

    使用时 importlib.import_module ,您不必指定“.py”。

    cube1 = Create(c1_x, c1_y, c1_z, file="block_model")
    

    特别是你应该通过 模块名称 (“block\u model”)作为参数, 非文件名 。当你经过时 "block_model.py" 作为论证,程序实际上是在解释 block_model 作为包名称(在您的情况下,它是不存在的)和 py 作为该软件包中的一个模块。阅读此 documentation 了解更多。