尝试导入自定义包时出现ModuleNotFoundError

时间:2020-09-09 01:39:20

标签: python python-packaging

玩自定义程序包,我遇到了障碍。我似乎无法使该特定程序包正常工作,尝试此操作时出现以下错误

from ImageSizer.sizer import ImageSizers
ImageSizers.get_image()

应该引发自定义NoParamSpecified错误,但引发

ModuleNotFoundError: No module named 'ImageSizer.sizer'; 'ImageSizer' is not a package

我尝试将'from ImageSizer.sizer Import ImageSizers'放入 init 中,但这也会引发ImportError。非常感谢您提供帮助以完成这项工作,我所希望的是能够为GUI导入此程序包并以更有效的方式调整图像大小。这是软件包(不包括当前为空的__init__py):

sizer.py

from PIL import ImageTk, Image
from ImageSizer import exceptions as ISE


# class to resize image for frames background
class ImageSizers:

    width = None
    height = None
    temp_image = None
    good_image = None
    param_set = False

    def set_params(self, width: int, height: int, image_path: str):
        self.width = int(width)
        self.height = int(height)
        self.temp_image = Image.open(image_path)
        self.good_image = ImageTk.PhotoImage(self.temp_image.resize((self.width, self.height)))
        self.param_set = True

    def get_image(self):
        if not self.param_set:
            raise ISE.NoParamSpecified("Please enter parameters")
        return self.good_image

    def get_width_and_height(self):
        if not self.param_set:
            raise ISE.NoParamSpecified("Please enter parameters")
        return [self.width, self.height]

    def change_size(self, new_width: int, new_height: int):
        if not self.param_set:
            raise ISE.NoParamSpecified("Please enter parameters")
        self.width = int(new_width)
        self.height = int(new_height)
        self.good_image = ImageTk.PhotoImage(self.temp_image.resize((self.width, self.height)))

    def change_filepath(self, image_path: str):
        if not self.param_set:
            raise ISE.NoParamSpecified("Please enter parameters")
        self.temp_image = Image.open(image_path)
        self.good_image = ImageTk.PhotoImage(self.temp_image.resize((self.width, self.height)))

exceptions.py

class ImageSizerExceptions(Exception):
    pass


class NoParamSpecified(ImageSizerExceptions):
    pass

我正在PyCharm的最新版本中使用python 3.8

编辑: 文件结构如下:

C:\MyWorkspace
    ImageSizer
        __init__.py
        exceptions.py
        sizer.py
    main
        test.py

0 个答案:

没有答案