AttributeError: Module 'pydantic' has no attribute '__version__' when running pyinstaller
AttributeError: Module 'pydantic' has no attribute '__version__' when running pyinstaller
我有一个 python 项目,在 Python 版本 3.9.1 上有一个 venv 运行。我在全球范围内安装了 pyinstaller(所以不在我的 venv 中),现在我正在尝试创建一个可执行文件。为此,我首先激活了我的虚拟环境并执行了命令:
pyinstaller --onefile --paths "C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages" edit_epub.py
这会传递路径参数,以便包含我在 venv 文件夹中安装的必要库。但是,该过程失败并出现以下错误:
pyinstaller --onefile --paths "C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages" edit_epub.py
77 INFO: PyInstaller: 4.6
77 INFO: Python: 3.9.1
78 INFO: Platform: Windows-10-10.0.19041-SP0
79 INFO: wrote C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.spec
83 INFO: UPX is not available.
86 INFO: Extending PYTHONPATH with paths
['C:\Users\hanne\Documents\Programme\add-stress-to-epub',
'C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages']
372 INFO: checking Analysis
384 INFO: Building because pathex changed
384 INFO: Initializing module dependency graph...
388 INFO: Caching module graph hooks...
400 INFO: Analyzing base_library.zip ...
3065 INFO: Processing pre-find module path hook distutils from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
3068 INFO: distutils: retargeting to non-venv dir 'c:\users\hanne\appdata\local\programs\python\python39\lib'
5047 INFO: Caching module dependency graph...
5221 INFO: running Analysis Analysis-00.toc
5224 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\hanne\appdata\local\programs\python\python39\python.exe
5467 INFO: Analyzing C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.py
6885 INFO: Processing pre-find module path hook site from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-site.py'.
6887 INFO: site: retargeting to fake-dir 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\fake-modules'
14927 INFO: Processing pre-safe import module hook six.moves from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-six.moves.py'.
24762 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py'.
26619 INFO: Processing module hooks...
26620 INFO: Loading module hook 'hook-certifi.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
26629 INFO: Loading module hook 'hook-IPython.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
27280 INFO: Loading module hook 'hook-jedi.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28458 INFO: Loading module hook 'hook-jinja2.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28460 INFO: Loading module hook 'hook-lxml.etree.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28462 INFO: Loading module hook 'hook-lxml.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28726 INFO: Loading module hook 'hook-parso.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28746 INFO: Loading module hook 'hook-pydantic.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
Traceback (most recent call last):
File "c:\users\hanne\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\hanne\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\hanne\AppData\Local\Programs\Python\Python39\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\__main__.py", line 124, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\__main__.py", line 58, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 782, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 714, in build
exec(code, spec_namespace)
File "C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.spec", line 7, in <module>
a = Analysis(['edit_epub.py'],
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 277, in __init__
self.__postinit__()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\datastruct.py", line 155, in __postinit__
self.assemble()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 445, in assemble
self.graph.process_post_graph_hooks(self)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\analysis.py", line 326, in process_post_graph_hooks
module_hook.post_graph(analysis)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\imphook.py", line 398, in post_graph
self._load_hook_module()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\imphook.py", line 361, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\compat.py", line 594, in importlib_load_source
return mod_loader.load_module()
File "<frozen importlib._bootstrap_external>", line 469, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 969, in load_module
File "<frozen importlib._bootstrap_external>", line 794, in load_module
File "<frozen importlib._bootstrap>", line 274, in _load_module_shim
File "<frozen importlib._bootstrap>", line 711, in _load
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py", line 35, in <module>
if not is_module_satisfies('pydantic >= 1.4'):
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 477, in is_module_satisfies
version = get_module_attribute(module_name, version_attr)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 340, in get_module_attribute
raise AttributeError('Module %r has no attribute %r' % (module_name, attr_name))
AttributeError: Module 'pydantic' has no attribute '__version__'
我已经安装了 2 个库(使用 pip):spaCy 和 BeautifulSoup。
此错误可能是什么原因造成的,我可以尝试如何修复它?
首先,我尝试用与这个 Github 问题相同的方式修复它:https://github.com/samuelcolvin/pydantic/issues/2572
但是,这没有用。所以我只是转到 appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py
下的文件并编辑文件以删除版本检查(简单地删除 if 条件并始终执行内容),从而修复了错误。
这不是最好的解决方案,但至少有效
我有一个 python 项目,在 Python 版本 3.9.1 上有一个 venv 运行。我在全球范围内安装了 pyinstaller(所以不在我的 venv 中),现在我正在尝试创建一个可执行文件。为此,我首先激活了我的虚拟环境并执行了命令:
pyinstaller --onefile --paths "C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages" edit_epub.py
这会传递路径参数,以便包含我在 venv 文件夹中安装的必要库。但是,该过程失败并出现以下错误:
pyinstaller --onefile --paths "C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages" edit_epub.py
77 INFO: PyInstaller: 4.6
77 INFO: Python: 3.9.1
78 INFO: Platform: Windows-10-10.0.19041-SP0
79 INFO: wrote C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.spec
83 INFO: UPX is not available.
86 INFO: Extending PYTHONPATH with paths
['C:\Users\hanne\Documents\Programme\add-stress-to-epub',
'C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages']
372 INFO: checking Analysis
384 INFO: Building because pathex changed
384 INFO: Initializing module dependency graph...
388 INFO: Caching module graph hooks...
400 INFO: Analyzing base_library.zip ...
3065 INFO: Processing pre-find module path hook distutils from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
3068 INFO: distutils: retargeting to non-venv dir 'c:\users\hanne\appdata\local\programs\python\python39\lib'
5047 INFO: Caching module dependency graph...
5221 INFO: running Analysis Analysis-00.toc
5224 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\hanne\appdata\local\programs\python\python39\python.exe
5467 INFO: Analyzing C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.py
6885 INFO: Processing pre-find module path hook site from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-site.py'.
6887 INFO: site: retargeting to fake-dir 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\fake-modules'
14927 INFO: Processing pre-safe import module hook six.moves from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-six.moves.py'.
24762 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py'.
26619 INFO: Processing module hooks...
26620 INFO: Loading module hook 'hook-certifi.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
26629 INFO: Loading module hook 'hook-IPython.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
27280 INFO: Loading module hook 'hook-jedi.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28458 INFO: Loading module hook 'hook-jinja2.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28460 INFO: Loading module hook 'hook-lxml.etree.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28462 INFO: Loading module hook 'hook-lxml.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28726 INFO: Loading module hook 'hook-parso.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
28746 INFO: Loading module hook 'hook-pydantic.py' from 'c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
Traceback (most recent call last):
File "c:\users\hanne\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\hanne\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\hanne\AppData\Local\Programs\Python\Python39\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\__main__.py", line 124, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\__main__.py", line 58, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 782, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 714, in build
exec(code, spec_namespace)
File "C:\Users\hanne\Documents\Programme\add-stress-to-epub\edit_epub.spec", line 7, in <module>
a = Analysis(['edit_epub.py'],
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 277, in __init__
self.__postinit__()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\datastruct.py", line 155, in __postinit__
self.assemble()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\building\build_main.py", line 445, in assemble
self.graph.process_post_graph_hooks(self)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\analysis.py", line 326, in process_post_graph_hooks
module_hook.post_graph(analysis)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\imphook.py", line 398, in post_graph
self._load_hook_module()
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\depend\imphook.py", line 361, in _load_hook_module
self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\compat.py", line 594, in importlib_load_source
return mod_loader.load_module()
File "<frozen importlib._bootstrap_external>", line 469, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 969, in load_module
File "<frozen importlib._bootstrap_external>", line 794, in load_module
File "<frozen importlib._bootstrap>", line 274, in _load_module_shim
File "<frozen importlib._bootstrap>", line 711, in _load
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py", line 35, in <module>
if not is_module_satisfies('pydantic >= 1.4'):
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 477, in is_module_satisfies
version = get_module_attribute(module_name, version_attr)
File "c:\users\hanne\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 340, in get_module_attribute
raise AttributeError('Module %r has no attribute %r' % (module_name, attr_name))
AttributeError: Module 'pydantic' has no attribute '__version__'
我已经安装了 2 个库(使用 pip):spaCy 和 BeautifulSoup。
此错误可能是什么原因造成的,我可以尝试如何修复它?
首先,我尝试用与这个 Github 问题相同的方式修复它:https://github.com/samuelcolvin/pydantic/issues/2572
但是,这没有用。所以我只是转到 appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py
下的文件并编辑文件以删除版本检查(简单地删除 if 条件并始终执行内容),从而修复了错误。
这不是最好的解决方案,但至少有效