Search code examples
pythonpython-3.xpyinstaller

Pyinstaller "failed to import the site module"


I've been trying to use PyInstaller for some time as it can turn a Python script into a single .exe file, instead of having a lib file and DLLs like cx_freeze does for example. So it's easier for my users to update the program when I push as it's "less" stuff to download.

However, when I run "pyinstaller CLI.py", I get the following error:

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.

Any help would be amazing.


Solution

  • Something is amiss with your Python installation. The "site-specific configuration hook" site.py is normally found in the Lib folder just underneath the Python install directory. But according to your error log, PyInstaller finds it in Lib/site-packages.

    See also this answer from 2014:

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

    It's referring to Python 2.7, but explains well what site.py does and still applies to newer Python versions, such as Python 3.9 here.

    Furthermore, the code line

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

    in your custom site.py is wrong. There is no (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 does not use the optional encoding argument.