使用 pip install 在库中包含图像

Including Images in a library with pip install

我正在构建一个基于 python 3.8 的自动化库,该库的某些部分需要 .PNG 文件来定位屏幕上的项目。

有没有办法做到这一点,或者这是否超出了 pip 的功能范围?

我试过从 git 仓库和本地目录安装 pip。 .PNG 文件永远不会被复制。

我尝试将 __ init __ py 放入包含图像的文件夹中,这使得它创建了图像所在的文件夹,但仍然没有复制图像。

我用谷歌搜索了这个问题,但我只知道有用于 PNG、图像和扩展的 pip 库。

有很多方法可以做到...

一种方法是在 setup.py

中使用 include_package_data
from setuptools import setup, find_packages
setup(
    ...
    include_package_data=True
)

并在 manifest.in

中包含数据文件

或更具体

from setuptools import setup, find_packages
setup(
    ...
    package_data={
        # If any package contains *.txt or *.rst files, include them:
        "": ["*.txt", "*.rst"],
        # And include any *.msg files found in the "hello" package, too:
        "hello": ["*.msg"],
    }
)

(另见:pypi setuptools documentation

另一种选择是将它们作为 base64 编码的字符串存储在 python 文件中))

images.py

img1 = "<SOMEB64ENCODED_STR>"

大多数包允许您将图像指定为原始字节或 base64 编码数据