Search code examples
pythonpyinstaller

AttributeError: Module 'pydantic' has no attribute '__version__' when running pyinstaller


I have a python project with a venv running on Python version 3.9.1. I installed pyinstaller globally (so not in my venv) and am now trying to create an executable. For this is I first activated my virtual environment and executed the command:

pyinstaller --onefile --paths "C:\Users\hanne\Documents\Programme\add-stress-to-epub\venv\Lib\site-packages" edit_epub.py

This passes the paths parameter in order to include the necessary libraries I installed in my venv folder. However, the process fails with following error:

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__'

I have installed 2 libraries (using pip): spaCy and BeautifulSoup.

What could be the cause of this error and what could I try to fix it?


Solution

  • First I tried to fix it the same way like in this Github issue: https://github.com/samuelcolvin/pydantic/issues/2572 However, this did not work. So I simply went to the file under appdata\local\programs\python\python39\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-pydantic.py and edited the file in order to remove the version checks (simply removed the if conditions and always executed the content), which fixed the errors.

    It is not the prettiest solution, but at least it worked