【问题标题】:Pyinstaller "failed to import the site module"Pyinstaller“无法导入站点模块”
【发布时间】:2021-08-05 07:51:15
【问题描述】:

我一直在尝试使用 PyInstaller,因为它可以将 Python 脚本转换为单个 .exe 文件,而不是像 cx_freeze 这样的 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 python-3.x pyinstaller


    【解决方案1】:

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

    另请参阅 2014 年的 this answer

    如果您在site-packages 中有一个site.py,那么这是一个错误,那里应该没有这样的文件。

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

    另外,代码行

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

    在您的自定义 site.py 中是错误的。没有名称为 'locale'(standard) encoding。这就是它提出LookupError 的原因。 same line of code in the site.py included with Python 3.9 不使用可选的 encoding 参数。

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多