当试图将 python 文件转换为带有 py安裝器的可执行文件时正在获取 TypeError
原标题:Getting TypeError when trying to convert a python file into an executable with pyinstaller
The intention is later on, to send questions via command lines to GPT4all and store the answer in a text document. I wanted to convert the prevent code to an exe, but it produced an TypeError.
Here is the code so far:
from gpt4all import GPT4All
model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", device= cpu ) # downloads / loads a 4.66GB LLM
with model.chat_session():
with open("C:\Temp\Antwort.txt", "a") as a:
print(model.generate("What is the capital of France?", max_tokens=1024), file=a)
That is the error:
(.venv) PS C:UserslbuntenbachPycharmProjectsGPT4all> pyinstaller --onefile Learning.py
1048 INFO: PyInstaller: 6.9.0, contrib hooks: 2024.7
1048 INFO: Python: 3.12.4
1115 INFO: Platform: Windows-10-10.0.19045-SP0
1115 INFO: Python environment: C:UserslbuntenbachPycharmProjectsGPT4all.venv
1131 INFO: wrote C:UserslbuntenbachPycharmProjectsGPT4allLearning.spec
1147 INFO: Module search paths (PYTHONPATH):
[ C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Scripts\pyinstaller.exe ,
C:\Users\lbuntenbach\AppData\Local\Programs\Python\Python312\python312.zip ,
C:\Users\lbuntenbach\AppData\Local\Programs\Python\Python312\DLLs ,
C:\Users\lbuntenbach\AppData\Local\Programs\Python\Python312\Lib ,
C:\Users\lbuntenbach\AppData\Local\Programs\Python\Python312 ,
C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv ,
C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages ,
C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\setuptools\_vendor ,
C:\Users\lbuntenbach\PycharmProjects\GPT4all ]
2100 INFO: checking Analysis
2100 INFO: Building Analysis because Analysis-00.toc is non existent
2100 INFO: Running Analysis Analysis-00.toc
2100 INFO: Target bytecode optimization level: 0
2100 INFO: Initializing module dependency graph...
2119 INFO: Caching module graph hooks...
2190 INFO: Analyzing base_library.zip ...
7332 INFO: Loading module hook hook-encodings.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
9867 INFO: Loading module hook hook-heapq.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
12568 INFO: Loading module hook hook-pickle.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
16389 INFO: Caching module dependency graph...
16721 INFO: Looking for Python shared library...
16754 INFO: Using Python shared library: C:UserslbuntenbachAppDataLocalProgramsPythonPython312python312.dll
16754 INFO: Analyzing C:UserslbuntenbachPycharmProjectsGPT4allLearning.py
16918 INFO: Loading module hook hook-platform.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
19333 INFO: Loading module hook hook-charset_normalizer.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks ...
19724 INFO: Loading module hook hook-certifi.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks ...
20570 INFO: Loading module hook hook-multiprocessing.util.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
20768 INFO: Loading module hook hook-xml.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
21469 INFO: Loading module hook hook-importlib_resources.py from C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks ...
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvScriptspyinstaller.exe\__main__.py", line 7, in
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstaller\__main__.py", line 231, in _console_script_run
run()
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstaller\__main__.py", line 215, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstaller\__main__.py", line 70, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstalleruildinguild_main.py", line 1216, in main
build(specfile, distpath, workpath, clean_build)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstalleruildinguild_main.py", line 1156, in build
exec(code, spec_namespace)
File "C:UserslbuntenbachPycharmProjectsGPT4allLearning.spec", line 4, in
a = Analysis(
^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstalleruildinguild_main.py", line 556, in __init__
self.__postinit__()
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstalleruildingdatastruct.py", line 184, in __postinit__
self.assemble()
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstalleruildinguild_main.py", line 696, in assemble
program_scripts.append(self.graph.add_script(script))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 268, in add_script
self._top_script_node = super().add_script(pathname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1160, in add_script
self._process_imports(n)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerlibmodulegraphmodulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 368, in _safe_import_hook
excluded_imports = self._find_all_excluded_imports(source_module.identifier)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependanalysis.py", line 356, in _find_all_excluded_imports
excluded_imports.update(module_hook.excludedimports)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependimphook.py", line 316, in __getattr__
self._load_hook_module()
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerdependimphook.py", line 383, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallercompat.py", line 583, in importlib_load_source
mod_loader.exec_module(mod)
File "", line 995, in exec_module
File "", line 488, in _call_with_frames_removed
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerhookshook-importlib_resources.py", line 18, in
if check_requirement("importlib_resources < 1.2.0"):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagesPyInstallerutilshooks\__init__.py", line 414, in check_requirement
return parsed_requirement.specifier.contains(version, prereleases=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagespackagingspecifiers.py", line 906, in contains
item = Version(item)
^^^^^^^^^^^^^
File "C:UserslbuntenbachPycharmProjectsGPT4all.venvLibsite-packagespackagingversion.py", line 200, in __init__
match = self._regex.search(version)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got NoneType
So far it seems, that the return is None, but I can t grasp, what ist producing the None value. Could someone explain the origin of the None value and is there a way to fix this issue? I m new to Python and grateful for every piece of advice.
问题回答
I got the same problem while working with pyinstaller and pandas. The only solution that worked in my case was to delete and recreate the venv.
It seems like importlib_resources version check in the PyInstaller hook is causing this issue.
Breaking the error down
The TypeError: expected string or bytes-like object, got NoneType indicates that a function is trying to process a None value where a string or bytes-like object is expected.
The specific line return parsed_requirement.specifier.contains(version, prereleases=True) in the error trace is where this occurs. Here, version is None, causing the TypeError.
Why could this happen?
The line if check_requirement("importlib_resources < 1.2.0") is intended to check if the installed version of importlib_resources is less than 1.2.0.
The function check_requirement fetches the version of the importlib_resources package. If the package is not installed, or its version information is missing or corrupted, check_requirement might return None.
Resolution
Ensure that importlib_resources is installed and its version is correctly recognized by the system. This should prevent None from being returned. Installing a specific version that matches the expected range can help. This could be done via pip install importlib_resources==1.1.0
If updating the package doesn’t resolve the issue, you might need to modify the hook-importlib_resources.py file. This should ultimately ensure that the hook handles None values gracefully and you end up resolving the error. For this you will need to locate the hook-importlib_resources.py file in your PyInstaller hooks directory and then in the following manner update it
# hook-importlib_resources.py
from PyInstaller.utils.hooks import check_requirement
try:
if check_requirement("importlib_resources < 1.2.0"):
...
except TypeError as e:
# Handle the case where version is None
print("Version check failed, possibly due to missing version info: ", e)
...