プログラミング素人のはてなブログ

プログラミングも電気回路も専門外の技術屋の末端が勉強したことや作品をアウトプットするブログ。コードに間違いなど見つけられたら、気軽にコメントください。 C#、Python3、ラズパイなど。

Pyinstallerでexeファイルの作成に失敗するとき

久しぶりにPythonを触っています

久しぶりにPyinstallerで実行ファイルを作ろうとしたのですが、以下のようなErrorができて作成できません。

C:\Users\XXXXs\Documents\Visual Studio 2017\Projects\Python\blueskyApi> pyinstaller 'c:\Users\XXXXs\Documents\Visual Studio 2017\Projects\Python\blueskyApi\a.py' --onefile
5964 INFO: PyInstaller: 5.12.0
5964 INFO: Python: 3.8.8 (conda)
5964 INFO: Platform: Windows-10-10.0.19041-SP0
5964 INFO: wrote C:\Users\XXXXs\Documents\Visual Studio 2017\Projects\Python\blueskyApi\a.spec
5998 INFO: UPX is not available.
6006 INFO: Extending PYTHONPATH with paths
['c:\\Users\\XXXXs\\Documents\\Visual Studio '
 '2017\\Projects\\Python\\blueskyApi']
9397 INFO: checking Analysis
9405 INFO: Building Analysis because Analysis-00.toc is non existent
9405 INFO: Initializing module dependency graph...
9422 INFO: Caching module graph hooks...
9588 INFO: Analyzing base_library.zip ...
26967 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\XXXXs\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
27815 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\XXXXs\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
37062 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\XXXXs\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
41244 INFO: Caching module dependency graph...
41710 INFO: running Analysis Analysis-00.toc
41772 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\XXXXs\anaconda3\python.exe
42610 INFO: Analyzing c:\Users\XXXXs\Documents\Visual Studio 2017\Projects\Python\blueskyApi\a.py
45526 INFO: Loading module hook 'hook-pydantic.py' from 'C:\\Users\\XXXXs\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
Traceback (most recent call last):
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 283, in get_module_attribute
    return _get_module_attribute(module_name, attr_name)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 403, in wrapped
    return call(function, *args, **kwargs)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 372, in call
    return isolated.call(function, *args, **kwargs)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\isolated\_parent.py", line 302, in call
    raise RuntimeError(f"Child process call to {function.__name__}() failed with:\n" + output)
RuntimeError: Child process call to _get_module_attribute() failed with:
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\isolated\_child.py", line 63, in run_next_command
    output = function(*args, **kwargs)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 279, in _get_module_attribute
    return getattr(module, attr_name)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\pydantic\__init__.py", line 386, in __getattr__
    return _getattr_migration(attr_name)
  File "C:\Users\XXXXs\anaconda3\lib\site-packages\pydantic\_migration.py", line 302, in wrapper
    raise PydanticImportError(f'`{import_path}` has been removed in V2.')
pydantic.errors.PydanticImportError: `pydantic:compiled` has been removed in V2.

For further information visit https://errors.pydantic.dev/2.7/u/import-error

The above exception was the direct cause of the following exception:

スクリプトの中身は、Blueskyの公式サンプルを持ってきた、以下のような最小限のものでも発生します。

from atproto import Client

client = Client(base_url='https://bsky.social/xrpc')
client.login('example.com', 'hunter2')
post = client.send_post('Hello world! I posted this via the Python SDK.')

docs.bsky.app

Errorの内容は、どうもPyinstallerに関して、pydanticというライブラリで出ているように見えます。

試行錯誤した結果、Python環境を最新Python 3.12.xにすることで解決しました。

今の今まで、Python 3.8.8を使っていました。

なんか、Python 3.5.2の残骸も残っているようですが…。
Anacondaの3.8.8環境を削除して、AnacondaではなくPythonの最新版 Python 3.12.xを入れました。
www.python.org

$ pip install pyinstaller
$ pip install atproto

まとめ

PythonでErrorが解消できないときはPython環境を最新にしてみましょう。
ついこの間、Python 3.10.xがでたような気がしていたのですが、いつの間にか12になっていました。
あまりにも古いのは互換性が保たれていないので、こういう機会にクリーンにしておくほうがよさそうです。