Pyinstaller:Exe 抛出 ModuleNotFoundError:没有名为“...”的模块

问题描述 投票:0回答:1

我是一名新程序员(6个月),第一次使用这个论坛。所以请允许我遗漏信息和不正确的条目。

我编写了自己的日志记录包,并喜欢将其导入我的另一个软件中。在 vscode 中,一切正常,我的包已正确加载并可以使用。在我的 venv 中,我可以在 configs 文件夹下看到文件 (logging_*.json),但我在可执行文件中缺少该文件。

我喜欢使用我的模块,而不用

--add-data
更改 pyinstaller 上的某些内容或添加生成规范文件。因为我希望有人导入我的模块并且不需要考虑类似的事情。

我将 github 上的存储库设置为公开:

日志存储库

这是我的日志记录项目的文件夹结构:

logging/
├── setup.py
└── bub_logger/
    ├── __init__.py
    ├── bub_logger.py
    ├── configs/
        ├── __init__.py
        ├── logging_console.json
        └── logging_file.json

我的Setup.py(目前不要使用轮子)

from setuptools import find_packages, setup

setup(
    name="bub_logger",
    version="0.0.1",
    packages=find_packages(),
    include_package_data=True,
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    description="A flexible and extendable logging module",
    install_requires=[
        "PyYAML",  # Für YAML-Unterstützung
    ],
    entry_points={
        "console_scripts": [
            "bub_logger=bub_logger:main",
        ],
    },
    package_data={
        "bub_logger": ["configs/*"],
    },
    author="-",
    author_email="-",
    url="-",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.7",
)

我如何在 bub_logger.py 中加载配置:

import importlib.resources as pkg_resources

load_config(...):
        config_data = None
        for ext in ["json", "yaml", "yml"]:
            try:
                config_file_path = pkg_resources.files("bub_logger.configs").joinpath(
                    f"{config_file}.{ext}"
                )
                with config_file_path.open("r") as file:
                    config_data = file.read()
                    if ext == "json":
                        config = json.loads(config_data)
                    else:
                        config = yaml.safe_load(config_data)
                    break
            except FileNotFoundError:
                continue

        if config_data is None:
            raise FileNotFoundError(f"Configuration file {config_file} not found.")
 ......

我在 vscode 中的任务是如何定义的,以使用 pyinstaller 创建可执行文件。它使用 .venv 解释器,因此它也应该加载已安装的包 -> bub_logger。

    {
      "label": "Build",
      "type": "shell",
      "command": "${workspaceFolder}\\.venv\\Scripts\\python",
      "args": [
        "-m",
        "PyInstaller",
        "--noconfirm",
        "--log-level=INFO",
        "--onefile",
        "--windowed",
        "--workpath=.\\BuildTmp",
        "--icon=..\\source\\images\\Prog.ico",
        "..\\openhmi.py"
      ],
      "options": {
        "cwd": "${workspaceFolder}/build/"
      },
      "dependsOrder": "sequence",
      "dependsOn": ["CompileImages", "mkdirBuild", "Clean"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": []
    },

因此,如果我运行构建过程并在此之后启动可执行文件,我会收到以下错误:

Traceback (most recent call last):
  File "openhmi.py", line 59, in <module>
  File "bub_logger\bub_logger.py", line 92, in load_config
  File "importlib\resources\_common.py", line 46, in wrapper
  File "importlib\resources\_common.py", line 56, in files
  File "functools.py", line 909, in wrapper
  File "importlib\resources\_common.py", line 82, in _
  File "importlib\__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'bub_logger.configs

在这里我们看到 bub_logger 是导入的并且是从可执行文件中使用的......它只是找不到所需的配置文件。

我做错了什么?我问了chatgpt和其他人工智能,但每个人工智能都只是给我返回相同的建议,这不起作用,而且人工智能无法再摆脱这个建议。

提前致谢。

我尝试通过以下方式获取可执行文件的内容: pyi-archive_viewer .\openhmi.exe > test.txt

但这并没有真正帮助我

Contents of 'openhmi.exe' (PKG/CArchive):
 position, length, uncompressed_length, is_compressed, typecode, name
 0, 233, 289, 1, 'm', 'struct'
 233, 3825, 7438, 1, 'm', 'pyimod01_archive'
 4058, 10014, 23131, 1, 'm', 'pyimod02_importers'
 14072, 2731, 6154, 1, 'm', 'pyimod03_ctypes'
 16803, 955, 1684, 1, 'm', 'pyimod04_pywin32'
 17758, 1020, 1835, 1, 's', 'pyiboot01_bootstrap'
 18778, 886, 1530, 1, 's', 'pyi_rth_inspect'
 19664, 677, 1224, 1, 's', 'pyi_rth_pyqt5'
 20341, 1313, 2315, 1, 's', 'pyi_rth_pkgutil'
 21654, 2061, 4058, 1, 's', 'pyi_rth_multiprocessing'
 23715, 1665, 2853, 1, 's', 'openhmi'
 25380, 187392, 590112, 1, 'b', 'PyQt5\\Qt5\\bin\\MSVCP140.dll'
 212772, 14704, 31728, 1, 'b', 'PyQt5\\Qt5\\bin\\MSVCP140_1.dll'
 227476, 2730495, 6023664, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Core.dll'
 2957971, 186854, 436720, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5DBus.dll'
 3144825, 2617501, 7008240, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Gui.dll'
 5762326, 568060, 1340400, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Network.dll'
 6330386, 1384258, 3591664, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Qml.dll'
 7714644, 188610, 438768, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5QmlModels.dll'
 7903254, 1620975, 4148720, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Quick.dll'
 9524229, 134827, 330736, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Svg.dll'
 9659056, 60405, 149488, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5WebSockets.dll'
 9719461, 2486436, 5498352, 1, 'b', 'PyQt5\\Qt5\\bin\\Qt5Widgets.dll'
 12205897, 22414, 44528, 1, 'b', 'PyQt5\\Qt5\\bin\\VCRUNTIME140_1.dll'
 12228311, 1750408, 4173928, 1, 'b', 'PyQt5\\Qt5\\bin\\d3dcompiler_47.dll'
 13978719, 11075, 25072, 1, 'b', 'PyQt5\\Qt5\\bin\\libEGL.dll'
 13989794, 1279082, 3385328, 1, 'b', 'PyQt5\\Qt5\\bin\\libGLESv2.dll'
 15268876, 7763398, 20923392, 1, 'b', 'PyQt5\\Qt5\\bin\\opengl32sw.dll'
 23032274, 30839, 68080, 1, 'b', 'PyQt5\\Qt5\\plugins\\generic\\qtuiotouchplugin.dll'
 23063113, 19828, 41968, 1, 'b', 'PyQt5\\Qt5\\plugins\\iconengines\\qsvgicon.dll'
 23082941, 19831, 39408, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qgif.dll'
 23102772, 21660, 45040, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qicns.dll'
 23124432, 18524, 38384, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qico.dll'
 23142956, 121065, 421360, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qjpeg.dll'
 23264021, 15397, 32240, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qsvg.dll'
 23279418, 15033, 31728, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qtga.dll'
 23294451, 136072, 390128, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qtiff.dll'
 23430523, 14444, 30192, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qwbmp.dll'
 23444967, 243280, 510448, 1, 'b', 'PyQt5\\Qt5\\plugins\\imageformats\\qwebp.dll'
 23688247, 447370, 844784, 1, 'b', 'PyQt5\\Qt5\\plugins\\platforms\\qminimal.dll'
 24135617, 402492, 754672, 1, 'b', 'PyQt5\\Qt5\\plugins\\platforms\\qoffscreen.dll'
 24538109, 188469, 482288, 1, 'b', 'PyQt5\\Qt5\\plugins\\platforms\\qwebgl.dll'
 24726578, 726834, 1477104, 1, 'b', 'PyQt5\\Qt5\\plugins\\platforms\\qwindows.dll'
 25453412, 29636, 68592, 1, 'b', 'PyQt5\\Qt5\\plugins\\platformthemes\\qxdgdesktopportal.dll'
 25483048, 68007, 144368, 1, 'b', 'PyQt5\\Qt5\\plugins\\styles\\qwindowsvistastyle.dll'
 25551055, 738447, 2482176, 1, 'b', 'PyQt5\\QtCore.pyd'
 26289502, 719707, 2490880, 1, 'b', 'PyQt5\\QtGui.pyd'
 27009209, 1313056, 5160960, 1, 'b', 'PyQt5\\QtWidgets.pyd'
 28322265, 55154, 120320, 1, 'b', 'PyQt5\\sip.cp312-win_amd64.pyd'
 28377419, 58206, 119192, 1, 'b', 'VCRUNTIME140.dll'
 28435625, 25850, 49528, 1, 'b', 'VCRUNTIME140_1.dll'
 28461475, 45480, 84760, 1, 'b', '_bz2.pyd'
 28506955, 58434, 125208, 1, 'b', '_ctypes.pyd'
 28565389, 120859, 252696, 1, 'b', '_decimal.pyd'
 28686248, 29946, 65816, 1, 'b', '_hashlib.pyd'
 28716194, 88542, 159512, 1, 'b', '_lzma.pyd'
 28804736, 19483, 35096, 1, 'b', '_multiprocessing.pyd'
 28824219, 17975, 32536, 1, 'b', '_queue.pyd'
 28842194, 39925, 83224, 1, 'b', '_socket.pyd'
 28882119, 70879, 177432, 1, 'b', '_ssl.pyd'
 28952998, 20138, 36632, 1, 'b', '_wmi.pyd'
 28973136, 1859475, 5191960, 1, 'b', 'libcrypto-3.dll'
 30832611, 23195, 39696, 1, 'b', 'libffi-8.dll'
 30855806, 282334, 787224, 1, 'b', 'libssl-3.dll'
 31138140, 96982, 199448, 1, 'b', 'pyexpat.pyd'
 31235122, 23160, 68376, 1, 'b', 'python3.dll'
 31258282, 2580097, 7009048, 1, 'b', 'python312.dll'
 33838379, 17476, 30488, 1, 'b', 'select.pyd'
 33855855, 417900, 1137944, 1, 'b', 'unicodedata.pyd'
 34273755, 92814, 222720, 1, 'b', 'yaml\\_yaml.cp312-win_amd64.pyd'
 34366569, 110, 130, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_ar.qm'
 34366679, 119, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_bg.qm'
 34366798, 119, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_ca.qm'
 34366917, 124, 157, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_cs.qm'
 34367041, 121, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_da.qm'
 34367162, 120, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_de.qm'
 34367282, 25, 16, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_en.qm'
 34367307, 118, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_es.qm'
 34367425, 77186, 293121, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_fa.qm'
 34444611, 97, 117, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_fi.qm'
 34444708, 119, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_fr.qm'
 34444827, 76, 70, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_gd.qm'
 34444903, 78978, 323590, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_gl.qm'
 34523881, 78, 83, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_he.qm'
 34523959, 2804, 8743, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_ar.qm'
 34526763, 3125, 10599, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_bg.qm'
 34529888, 2224, 7444, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_ca.qm'
 34532112, 3810, 15297, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_cs.qm'
 34535922, 1524, 4795, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_da.qm'
 34537446, 2311, 7570, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_de.qm'
 34539757, 25, 16, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_en.qm'
 34539782, 3103, 10704, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_es.qm'
 34542885, 3090, 10922, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_fr.qm'
 34545975, 3187, 10891, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_gl.qm'
 34549162, 3114, 10284, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_hu.qm'
 34552276, 3050, 10612, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_it.qm'
 34555326, 2760, 7917, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_ja.qm'
 34558086, 2034, 5708, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_ko.qm'
 34560120, 2882, 9673, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_pl.qm'
 34563002, 2282, 7288, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_ru.qm'
 34565284, 3260, 10388, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_sk.qm'
 34568544, 3181, 10363, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_sl.qm'
 34571725, 1564, 4629, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_tr.qm'
 34573289, 3108, 9750, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_uk.qm'
 34576397, 2531, 6441, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_zh_CN.qm'
 34578928, 3150, 9301, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_help_zh_TW.qm'
 34582078, 114, 146, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_hu.qm'
 34582192, 119, 153, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_it.qm'
 34582311, 115, 146, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_ja.qm'
 34582426, 115, 146, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_ko.qm'
 34582541, 46643, 165383, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_lt.qm'
 34629184, 86, 89, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_lv.qm'
 34629270, 128, 161, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_pl.qm'
 34629398, 18980, 70334, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_pt.qm'
 34648378, 130, 164, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_ru.qm'
 34648508, 124, 157, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_sk.qm'
 34648632, 61702, 228428, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_sl.qm'
 34710334, 18206, 65851, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_sv.qm'
 34728540, 91, 110, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_tr.qm'
 34728631, 131, 164, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_uk.qm'
 34728762, 40216, 117347, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_zh_CN.qm'
 34768978, 107, 141, 1, 'x', 'PyQt5\\Qt5\\translations\\qt_zh_TW.qm'
 34769085, 48163, 160017, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_ar.qm'
 34817248, 47497, 165337, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_bg.qm'
 34864745, 57942, 210159, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_ca.qm'
 34922687, 50353, 174701, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_cs.qm'
 34973040, 51815, 181387, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_da.qm'
 35024855, 59624, 220467, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_de.qm'
 35084479, 25, 16, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_en.qm'
 35084504, 46937, 165170, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_es.qm'
 35131441, 50753, 179941, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_fi.qm'
 35182194, 46368, 166167, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_fr.qm'
 35228562, 50693, 189580, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_gd.qm'
 35279255, 42852, 138690, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_he.qm'
 35322107, 46650, 160494, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_hu.qm'
 35368757, 45046, 161172, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_it.qm'
 35413803, 43169, 129911, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_ja.qm'
 35456972, 52269, 156799, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_ko.qm'
 35509241, 44974, 153608, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_lv.qm'
 35554215, 46899, 162982, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_pl.qm'
 35601114, 58556, 203767, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_ru.qm'
 35659670, 37339, 125763, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_sk.qm'
 35697009, 56449, 194487, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_tr.qm'
 35753458, 46633, 158274, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_uk.qm'
 35800091, 45464, 127849, 1, 'x', 'PyQt5\\Qt5\\translations\\qtbase_zh_TW.qm'
 35845555, 544159, 1703997, 1, 'x', 'base_library.zip'
 36389714, 157527, 290282, 1, 'x', 'certifi\\cacert.pem'
 36547241, 8, 0, 1, 'x', 'certifi\\py.typed'
 36547249, 2317932, 2317932, 0, 'z', 'PYZ-00.pyz'
? ? 

python pyinstaller python-module
1个回答
0
投票

我也是新手,但我确实遇到了 python 包(pyodbc)的类似问题,并发现我必须使用“--hidden-import“pyodbc””手动添加它。由于某种原因,这只是 pyinstaller 软件包无法获取。

© www.soinside.com 2019 - 2024. All rights reserved.