安装程序 "failed to import the site module"

Pyinstaller "failed to import the site module"

我一直在尝试使用 PyInstaller 一段时间,因为它可以将 Python 脚本转换为单个 .exe 文件,而不是像 [=13= 这样的 lib 文件和 DLL ] 例如。因此,当我推送时,我的用户可以更轻松地更新程序,因为下载的东西“更少”。

但是,当我 运行 "pyinstaller CLI.py" 时,出现以下错误:

76 INFO: PyInstaller: 4.3
76 INFO: Python: 3.9.5
87 INFO: Platform: Windows-10-10.0.19042-SP0
88 INFO: wrote C:\Users\Admin\Desktop\UnknownAIO\CLI.spec
91 INFO: UPX is not available.
92 INFO: Extending PYTHONPATH with paths
['C:\Users\Admin\Desktop\UnknownAIO',
 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\Lib\site-packages\site.py',
 'C:\Users\Admin\Desktop\UnknownAIO']
104 INFO: checking Analysis
104 INFO: Building Analysis because Analysis-00.toc is non existent
104 INFO: Initializing module dependency graph...
108 INFO: Caching module graph hooks...
115 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
119 INFO: Analyzing base_library.zip ...
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 617, in <module>
    main()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 604, in main
    known_paths = addsitepackages(known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 387, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 226, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 175, in addpackage
    f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
LookupError: unknown encoding: locale
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 617, in <module>
    main()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 604, in main
    known_paths = addsitepackages(known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 387, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 226, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 175, in addpackage
    f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
LookupError: unknown encoding: locale
2115 INFO: Processing pre-find module path hook distutils from 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
2116 INFO: distutils: retargeting to non-venv dir 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib'
4439 INFO: Caching module dependency graph...
4528 INFO: running Analysis Analysis-00.toc
4542 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\admin\appdata\local\programs\python\python39\python.exe
4604 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\users\admin\appdata\local\programs\python\python39\python39.dll
4614 INFO: Analyzing C:\Users\Admin\Desktop\UnknownAIO\CLI.py
5032 INFO: Processing pre-find module path hook site from 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-site.py'.
5033 INFO: site: retargeting to fake-dir 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\fake-modules'
8190 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py'.
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 617, in <module>
    main()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 604, in main
    known_paths = addsitepackages(known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 387, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 226, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\site.py", line 175, in addpackage
    f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
LookupError: unknown encoding: locale
pre-safe-import-module hook failed, needs fixing.

任何帮助都会很棒。

您的 Python 安装有问题。 "site-specific configuration hook" site.py 通常位于 Python 安装目录下的 Lib 文件夹中。但是根据您的错误日志,PyInstaller 在 Lib/site-packages.

中找到了它

另见 this answer 2014 年:

If you have a site.py in site-packages then that is an error, there should be no such file there.

它指的是 Python 2.7,但很好地解释了 site.py 的作用并且仍然适用于较新的 Python 版本,例如此处的 Python 3.9。

此外,代码行

f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")

你的习惯 site.py 是错误的。没有 (standard) encoding with the name 'locale'. Which is why it raises that LookupError. That same line of code in the site.py included with Python 3.9 不使用可选的 encoding 参数。