稍后的目的是通过命令行向 GPT4all 发送问题并将答案存储在文本文档中。 我想将阻止代码转换为 exe,但它产生了 TypeError。
这是到目前为止的代码:
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)
这就是错误:
(.venv) PS C:\Users\lbuntenbach\PycharmProjects\GPT4all> 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:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv
1131 INFO: wrote C:\Users\lbuntenbach\PycharmProjects\GPT4all\Learning.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:\Users\lbuntenbach\AppData\Local\Programs\Python\Python312\python312.dll
16754 INFO: Analyzing C:\Users\lbuntenbach\PycharmProjects\GPT4all\Learning.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 "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\__main__.py", line 231, in _console_script_run
run()
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\__main__.py", line 215, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\__main__.py", line 70, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\building\build_main.py", line 1216, in main
build(specfile, distpath, workpath, clean_build)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\building\build_main.py", line 1156, in build
exec(code, spec_namespace)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\Learning.spec", line 4, in <module>
a = Analysis(
^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\building\build_main.py", line 556, in __init__
self.__postinit__()
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
self.assemble()
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\building\build_main.py", line 696, in assemble
program_scripts.append(self.graph.add_script(script))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 268, in add_script
self._top_script_node = super().add_script(pathname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1160, in add_script
self._process_imports(n)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 437, in _safe_import_hook
ret_modules = super()._safe_import_hook(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2048, in _safe_import_hook
target_modules = self.import_hook(
^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1232, in import_hook
target_package, target_module_partname = self._find_head_package(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1411, in _find_head_package
target_package = self._safe_import_module(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 501, in _safe_import_module
return super()._safe_import_module(module_basename, module_name, parent_package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1792, in _safe_import_module
self._process_imports(n)
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2591, in _process_imports
target_modules = self._safe_import_hook(*import_info, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 368, in _safe_import_hook
excluded_imports = self._find_all_excluded_imports(source_module.identifier)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\analysis.py", line 356, in _find_all_excluded_imports
excluded_imports.update(module_hook.excludedimports)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\imphook.py", line 316, in __getattr__
self._load_hook_module()
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\depend\imphook.py", line 383, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\compat.py", line 583, in importlib_load_source
mod_loader.exec_module(mod)
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\hooks\hook-importlib_resources.py", line 18, in <module>
if check_requirement("importlib_resources < 1.2.0"):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 414, in check_requirement
return parsed_requirement.specifier.contains(version, prereleases=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\packaging\specifiers.py", line 906, in contains
item = Version(item)
^^^^^^^^^^^^^
File "C:\Users\lbuntenbach\PycharmProjects\GPT4all\.venv\Lib\site-packages\packaging\version.py", line 200, in __init__
match = self._regex.search(version)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'
到目前为止,返回似乎是 None,但是我无法理解是什么产生了 None 值。 有人可以解释 None 值的起源吗?有没有办法解决这个问题? 我是 Python 新手,感谢每一条建议。
这个错误与
gpt4all
库本身有关,而非你的代码。看起来像是 PyInstaller 试图分析你的依赖,并在
importlib_resources
的版本检查中遇到了问题。
你可以尝试以下步骤来解决此问题:
1. 更新相关库
首先,确保你使用的库都是最新版本。这可能修复版本冲突或已知错误。
pip install --upgrade pyinstaller packaging importlib_resources gpt4all
2. 指定
importlib_resources
版本
如果更新后问题仍然存在,尝试在你的代码中明确指定
importlib_resources
版本。这可以帮助 PyInstaller 正确分析依赖关系。
在你的脚本顶部添加以下内容:
import importlib.metadata
try:
version = importlib.metadata.version('importlib_resources')
except importlib.metadata.PackageNotFoundError:
# Package is not installed.
version = None
if version is not None and version.startswith("5."):
import importlib_resources
else:
import importlib_resources as pkg_resources
然后,在你的代码中,将所有对
importlib_resources
的引用替换为
pkg_resources
。
3. 使用
--hidden-import
参数
如果以上方法都无法解决问题,你可以尝试使用 PyInstaller 的
--hidden-import
参数手动指定
importlib_resources
。
pyinstaller --onefile --hidden-import=importlib_resources Learning.py
4. 使用虚拟环境
为了避免依赖冲突,建议在虚拟环境中运行你的项目。
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt # 安装项目依赖
pyinstaller --onefile Learning.py
5. 报告问题
如果问题仍然存在,请在 PyInstaller 或
gpt4all
的 GitHub 存储库中提交问题,并提供详细的错误信息和你的环境配置。这可以帮助开发者识别并修复问题。
请注意,
gpt4all
库的开发非常活跃,可能存在一些尚未解决的兼容性问题。如果以上方法都无法解决问题,你可以考虑使用其他 GPT-4 库或等待
gpt4all
发布更新版本。