在 makefile 上构建 python 包时出错,即使我已经安装了包

Error building python package on makefile even though I have the package installed

我正在尝试运行 为某个项目生成文件,但是在生成 运行 期间我遇到了很多错误,即使在 运行 初始化 make 时一切都成功了。目前我 运行 这个 makefile 在 conda 环境下

我什至检查了有错误的文件并且 运行 它直接使用 python blablabla.py 并且当我 运行 时它 运行 完美makefile 它在​​同一个文件上显示为错误

all: build

init:
    @which python3 > /dev/null 2>&1 || (echo "python3 is not installed on this system" && exit 1)
    @which pip3 > /dev/null 2>&1 || (echo "pip3 is not installed on this system" && exit 1)
    @echo "Installing requirements with pip3..."
    @while read package; \
    do \
      line="$$package"; \
      [[ "$$line" == git+* ]] && \
        line=`echo "$$line" | sed -e 's/git.*\///g' -e 's/.git$$//g'`; \
      pip3 freeze 2>/dev/null | grep -P `echo "$$line" | sed 's/>=/.*/g'` > /dev/null 2>&1 || \
        pip3 install --user "$$package"; \
    done < requirements.txt

build: init build-lang
    @echo "Building python package..."
    @mypy --package color_harmonization
    @echo "#! /bin/bash" > color-harmonization
    @echo "/usr/bin/env python3 -m color_harmonization" >> color-harmonization
    @chmod +x color-harmonization

run: build
    @echo "Running application..."
    @./color-harmonization

lang:
    @echo "Generating internationalization files..."
    @mkdir -p color_harmonization/gui/locale/de_DE/LC_MESSAGES
    @intltool-extract --type=gettext/glade color_harmonization/gui/color-harmonization.glade
    @xgettext --from-code=UTF-8 --language=Python --keyword=_ --keyword=N_ \
    --output=color_harmonization/gui/locale/locale.pot \
    `find . -name "*.py" -type f` \
    color_harmonization/gui/color-harmonization.glade.h
    @msginit --input=color_harmonization/gui/locale/locale.pot \
    --locale=de_DE.UTF-8 --output-file=color_harmonization/gui/locale/de_DE/LC_MESSAGES/de_DE.po

build-lang:
    @echo "Compiling internationalization files..."
    @msgfmt --output color_harmonization/gui/locale/de_DE/LC_MESSAGES/color_harmonization.mo \
    color_harmonization/gui/locale/de_DE/LC_MESSAGES/de_DE.po

这些是构建 makefile 时的错误

Compiling internationalization files...
Building python package...
color_harmonization/gui/gl_widget.py:21: error: No library stub file for module 'gi.repository'
color_harmonization/gui/gl_widget.py:22: error: Cannot find implementation or library stub for module named 'OpenGL'
color_harmonization/gui/__init__.py:19: error: Name 'List' is not defined
color_harmonization/gui/__init__.py:19: note: Did you forget to import it from "typing"? (Suggestion: "from typing import List")
color_harmonization/__init__.py:19: error: Name 'List' is not defined
color_harmonization/__init__.py:19: note: Did you forget to import it from "typing"? (Suggestion: "from typing import List")
color_harmonization/handler.py:20: error: No library stub file for module 'gi.repository'
color_harmonization/gui/hue_sat_wheel_widget.py:20: error: No library stub file for module 'numpy'
color_harmonization/gui/hue_sat_wheel_widget.py:22: error: Cannot find implementation or library stub for module named 'cairocffi'
color_harmonization/gui/hue_sat_wheel_widget.py:25: error: No library stub file for module 'gi.repository'
color_harmonization/gui/gl_quad_renderer.py:19: error: No library stub file for module 'numpy'
color_harmonization/gui/gl_quad_renderer.py:23: error: Cannot find implementation or library stub for module named 'OpenGL'
color_harmonization/gui/gl_quad_renderer.py:24: error: Cannot find implementation or library stub for module named 'pyrr'
color_harmonization/gui/gl_quad_renderer.py:25: error: No library stub file for module 'PIL'
color_harmonization/gui/gl_image.py:19: error: No library stub file for module 'gi.repository'
color_harmonization/gui/assistant.py:24: error: No library stub file for module 'gi.repository'
color_harmonization/gui/assistant.py:24: note: (Stub files are from https://github.com/python/typeshed)
color_harmonization/gui/assistant.py:39: error: Module has no attribute "bindtextdomain"
color_harmonization/gui/assistant.py:104: error: Incompatible types in assignment (expression has type "None", variable has type "List[str]")
color_harmonization/gui/assistant.py:105: error: Incompatible types in assignment (expression has type "None", variable has type "str")
color_harmonization/__main__.py:22: error: Cannot find implementation or library stub for module named 'gi'
color_harmonization/__main__.py:22: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 18 errors in 9 files (checked 12 source files)
Makefile:33: recipe for target 'build' failed
make: *** [build] Error 1

这是 python 文件中的一个,因为它有很多,我只 post 第一个 python 文件出错

import abc

from gi.repository import Gtk, Gdk
from OpenGL import GL

class GLRenderer (metaclass = abc.ABCMeta):
    @property
    def gl_widget (self: 'GLRenderer') -> 'GLWidget':
        return self.__gl_widget

    @gl_widget.setter
    def gl_widget (self: 'GLRenderer', value: 'GLWidget') -> None:
        self.__gl_widget = value

    def make_current (self: 'GLRenderer') -> None:
        self.gl_widget.make_current ()

    @abc.abstractmethod
    def load (self: 'GLRenderer') -> None:
        pass

    @abc.abstractmethod
    def update (self: 'GLRenderer') -> None:
        pass

    @abc.abstractmethod
    def resize (self: 'GLRenderer', width: int, height: int) -> None:
        GL.glViewport (0, 0, width, height)

    @abc.abstractmethod
    def render (self: 'GLRenderer') -> None:
        GL.glClear (GL.GL_COLOR_BUFFER_BIT)
        GL.glClearColor (0, 0, 0, 0)

class GLWidget (Gtk.Overlay):
    def __init__ (self: 'GLWidget', renderer: GLRenderer,
                  gl_major_version: int, gl_minor_version: int) -> None:
        super ().__init__ ()

        self.renderer = renderer
        self.renderer.gl_widget = self

        self.gl_area = Gtk.GLArea ()
        self.gl_area.set_required_version (gl_major_version, gl_minor_version)
        self.gl_area.props.has_alpha = True
        self.gl_area.props.has_depth_buffer = False
        self.gl_area.props.auto_render = True
        self.gl_area.connect ('render', self.__handle_render)
        self.gl_area.connect ('realize', self.__handle_realize)
        self.gl_area.connect ('resize', self.__handle_resize)

        self.add_overlay (self.gl_area)
        self.props.expand = True

        self.__width = 400
        self.__height = 300

    def __handle_render (self: 'GLWidget', gl_area: Gtk.GLArea, context: Gdk.GLContext) -> None:
        self.renderer.render ()

    def __handle_realize (self: 'GLWidget', gl_area: Gtk.GLArea) -> None:
        self.renderer.load ()

    def __handle_resize (self: 'GLWidget', gl_area: Gtk.GLArea, width: int, height: int) -> None:
        self.__width = width
        self.__height = height
        self.renderer.resize (width, height)

    @property
    def width (self: 'GLWidget') -> int:
        return self.__width

    @property
    def height (self: 'GLWidget') -> int:
        return self.__height

    def make_current (self: 'GLWidget') -> None:
        self.gl_area.make_current ()

请查看此 github link 以获得完整的源代码。如果有任何建议和帮助,我将不胜感激

问题似乎是 mypy 没有找到模块。当您 运行 python gl_widget.py 没有导入错误时,这些模块似乎在您的系统上安装得很好。

请学习 mypy 手册并执行其中提到的步骤:https://mypy.readthedocs.io/en/stable/running_mypy.html#finding-imports

如果这不能解决您的问题或者您仍有疑问,请提出另一个问题。这次没有标记 makefile,只有 mypy 和 python。因为这不是 makefile 问题,而是 mypy 和 python 模块查找问题。

此外,由于您的用户名是 "wanttobepro",学习做最少的可重现示例对您成为专业人士非常有帮助。请学习做最少的可重现示例。

阅读此处了解更多信息:https://whosebug.com/help/minimal-reproducible-example

甚至更多:https://duckduckgo.com/?q=minimal+reproducible+examples&t=ffab&ia=web