☂️

PyInstallerがうまく動かないときの回避策

2024/06/04に公開

本記事は、 https://dev.to/kazto/pyinstaller-failed-1ph0 の日本語訳です。

2024/6/4 現在、PyInstallerを実行すると、以下のようなエラーが発生します。

> pyinstaller -F .\main.py
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\kazto\work\MyProject\.venv\Scripts\pyinstaller.exe\__main__.py", line 8, in <module>
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\__main__.py", line 228, in _console_script_run
    run()
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\__main__.py", line 170, in run
    parser = generate_parser()
             ^^^^^^^^^^^^^^^^^
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\__main__.py", line 136, in generate_parser
    import PyInstaller.building.build_main
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\building\build_main.py", line 28, in <module>
    from PyInstaller.building.api import COLLECT, EXE, MERGE, PYZ
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\building\api.py", line 32, in <module>
    from PyInstaller.building.splash import Splash  # argument type validation in EXE
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\building\splash.py", line 23, in <module>
    from PyInstaller.depend import bindepend
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\depend\bindepend.py", line 25, in <module>
    from PyInstaller.depend import dylib, utils
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\depend\utils.py", line 31, in <module>
    from PyInstaller.lib.modulegraph import modulegraph
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 34, in <module>
    from altgraph.ObjectGraph import ObjectGraph
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\altgraph\__init__.py", line 144, in <module>
    __version__ = pkg_resources.require("altgraph")[0].version
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\pkg_resources\__init__.py", line 926, in require
    needed = self.resolve(parse_requirements(requirements))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\pkg_resources\__init__.py", line 787, in resolve
    dist = self._resolve_dist(
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\pkg_resources\__init__.py", line 828, in _resolve_dist
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'altgraph' distribution was not found and is required by the application

このエラーは、以下の部分が原因です。

  File "C:\Users\kazto\work\MyProject\.venv\Lib\site-packages\altgraph\__init__.py", line 144, in <module>
    __version__ = pkg_resources.require("altgraph")[0].version
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pkg_resouces は現在ではObsoleteなAPIだそうで、require()メソッドは正しく動作しません。

本来、altgraph パッケージが修正されるべきなのですが、暫定的に以下のように回避しました。

  1. .venv\Lib\site-packages\pkg_resources_init_.py を開く
  2. __version__ 変数を __version__ = "0.17.4" のように、インストールしたバージョンに書き換える

どなたか詳しい方がいらっしゃったら、pkg_resoucesの代わりに何を使うべきなのか、情報をお待ちしております。

Discussion