使用 Python 的 PyInstaller:3.10.0b4 - 导入错误:没有名为 _bootlocale 的模块

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

我安装了 Python 10 Beta 4(我必须使用它,因为我需要仅在 v3.10 中提供的模式匹配功能):

C:\Users\myname\Documents\Projects\Project Migration\SeleniumExamplePy>py -3 --version
Python 3.10.0b4

我确保我的 pyinstaller 是最新的:

    C:\Users\myname\Documents\Projects\Project Migration\SeleniumExamplePy>pip install --upgrade pyinstaller
Requirement already satisfied: pyinstaller in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (4.4)
Requirement already satisfied: pefile>=2017.8.1 in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (2021.5.24)
Requirement already satisfied: pyinstaller-hooks-contrib>=2020.6 in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (2021.2)
Requirement already satisfied: pywin32-ctypes>=0.2.0 in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: setuptools in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (56.0.0)
Requirement already satisfied: altgraph in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (0.17)
Requirement already satisfied: future in c:\users\myname\appdata\local\programs\python\python310\lib\site-packages (from pefile>=2017.8.1->pyinstaller) (0.18.2)

我在尝试将经过彻底测试的 python 代码转换为可执行文件时遇到 ImportError: No module named _bootlocale 错误:

**C:\Users\myname\Documents\Projects\Project Migration\SeleniumExamplePy>pyinstaller --onefile --clean ExtractRules.spec**
83 INFO: PyInstaller: 4.4
83 INFO: Python: 3.10.0b4
100 INFO: Platform: Windows-10-10.0.18363-SP0
110 INFO: UPX is not available.
110 INFO: Removing temporary files and cleaning cache in C:\Users\myname\AppData\Local\pyinstaller
113 INFO: Extending PYTHONPATH with paths
['C:\\Users\\myname\\Documents\\Projects\\Project '
 'Migration\\SeleniumExamplePy',
 'C:\\Users\\myname\\Documents\\Projects\\Project '
 'Migration\\SeleniumExamplePy']
126 INFO: checking Analysis
126 INFO: Building Analysis because Analysis-00.toc is non existent
126 INFO: Initializing module dependency graph...
126 INFO: Caching module graph hooks...
139 INFO: Analyzing base_library.zip ...
3449 INFO: Processing pre-find module path hook distutils from 'c:\\users\\myname\\appdata\\local\\programs\\python\\python310\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
3449 INFO: distutils: retargeting to non-venv dir 'c:\\users\\myname\\appdata\\local\\programs\\python\\python310\\lib'
Traceback (most recent call last):
  File "c:\users\myname\appdata\local\programs\python\python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\myname\appdata\local\programs\python\python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\myname\AppData\Local\Programs\Python\Python310\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\__main__.py", line 126, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\building\build_main.py", line 758, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\building\build_main.py", line 705, in build
    exec(code, spec_namespace)
  File "ExtractRules.spec", line 7, in <module>
    a = Analysis(['ExtractRules.py'],
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\building\build_main.py", line 254, in __init__
    self.__postinit__()
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\building\datastruct.py", line 159, in __postinit__
    self.assemble()
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\building\build_main.py", line 354, in assemble
    self.graph = initialize_modgraph(
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\depend\analysis.py", line 882, in initialize_modgraph
    graph = PyiModuleGraph(
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\depend\analysis.py", line 122, in __init__
    self._analyze_base_modules()
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\depend\analysis.py", line 289, in _analyze_base_modules
    self._base_modules = [mod
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\depend\analysis.py", line 291, in <listcomp>
    for mod in self.import_hook(req)]
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1505, in import_hook
    target_package, target_module_partname = self._find_head_package(
  File "c:\users\myname\appdata\local\programs\python\python310\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1711, in _find_head_package
    raise ImportError("No module named " + target_package_name)
ImportError: No module named _bootlocale

我看了这个评论 https://bugzilla.redhat.com/show_bug.cgi?id=1899950 AzyCrw4282发布了链接以回答非常类似的问题PyInstaller - ImportError:没有名为_bootlocale的模块 Victor Stinner 于 2021-01-19 10:25:32 UTC

发表声明

“哦,_bootlocale 是一个私有模块,不应导入 直接地。我将其删除并更换为新的 Python 3.10 中的 _locale._get_locale_encoding() 函数”

那么,如果模块被删除,为什么最新的 pyinstaller 仍然尝试将其拉入? 是因为我使用的是 Python 3.10b4 并且 pyinstaller 尚未更新吗? 有没有办法从可执行文件构建中排除 _bootlocale 模块以便能够构建可执行文件?

我详尽地测试了我的程序,从 .py 运行时它没有任何问题。我不会调用任何直接引用 bootlocale 模块的东西,也不能 100% 确定它在从 3.10 中删除之前做了什么。

python pyinstaller importerror python-3.10
8个回答
23
投票

这是一个Python 3.10兼容性问题,here提到过。

您可以通过在命令中添加

--exclude-module _bootlocale
来解决该问题。

例如:

pyinstaller.exe app.py --exclude-module _bootlocale

12
投票

有同样的问题,你只需要安装 pyinstaller,然后安装 3.10 支持。

用途:

pip install pyinstaller
pip install https://github.com/rokm/pyinstaller/archive/refs/heads/python-3.10.zip

5
投票

尝试安装最新版本的 Pyinstaller
这解决了我的问题:

pip install pyinstaller==4.10

4
投票

这似乎是 pyinstaller 和 python 之间的版本不匹配。我最近升级到python3.10,从requirements.txt安装,然后遇到了这个问题。我正在运行 pyinstaller 4.5.1,但最新的是 pyinstaller 5.1。您可以通过执行以下操作来获取最新的 pyinstaller:

pip install --upgrade pyinstaller

python -m pip install --upgrade pyinstaller

2
投票

首先使用此命令安装

pyinstaller

pip install pyinstaller==4.10

或者如果您想安装最新版本的

pyinstaller
,请使用:

pip install pyinstaller

安装后尝试再次创建您的exe,它将起作用。


1
投票

就我而言,我通过卸载 fbs 包解决了这个问题

pip3 uninstall fbs

1
投票

我通过编辑 pyproject.toml 在我的诗歌项目中修复了它:

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.dependencies]
python = "^3.8,<3.11"

0
投票

我使用了这个自定义的 pyconverter 工具,它运行完美。该工具的链接 https://github.com/sazalo101/py-to-exe-or-linux-executable/

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