[Python]ImportError: DLL load failed while importing _cext エラー

Python

Pythonパッケージを利用する際に発生したエラーと対応方法について、備忘録として残しておきます。

サマリとしては、

  • Pythonバージョンによっては利用できないパッケージが存在すること
  • 対応しているPythonバージョンの確認の仕方

を抑えておく必要があります。

wordcloudパッケージを利用時に遭遇したエラーで、wordcloudパッケージは正常にインストールできたのですが、import時に発生しました。

Windows11環境上のPython 3.12で試していた内容です。

>pip install wordcloud
Collecting wordcloud
  Downloading wordcloud-1.9.3-cp312-cp312-win_amd64.whl.metadata (3.5 kB)
  :
Installing collected packages: six, pyparsing, pillow, packaging, numpy, kiwisolver, fonttools, cycler, python-dateutil, contourpy, matplotlib, wordcloud
Successfully installed contourpy-1.2.0 cycler-0.12.1 fonttools-4.49.0 kiwisolver-1.4.5 matplotlib-3.8.3 numpy-1.26.4 packaging-23.2 pillow-10.2.0 pyparsing-3.1.1 python-dateutil-2.8.2 six-1.16.0 wordcloud-1.9.3

>python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wordcloud
  :
  File "C:\Users\xxxxxxxx\AppData\Local\Programs\Python\Python312\Lib\site-packages\kiwisolver\__init__.py", line 8, in <module>
    from ._cext import (
ImportError: DLL load failed while importing _cext: 指定されたモジュールが見つかりません。

インターネット上で調べると、

'_cext' - no such module when importing OrbitalPy
I'm using the OrbitalPy lib to display orbits in Python. However, when I run the program (which is actually listed in the docs/examples of OrbitalPy): from nump...

がヒットし、msvc-runtimeパッケージが追加で必要であることが分かります。

早速インストールを試みたところ、今度は以下のエラーに遭遇しました。どうやらmsvc-runtimeパッケージが存在しない様子。

>pip install msvc-runtime
ERROR: Could not find a version that satisfies the requirement msvc-runtime (from versions: none)
ERROR: No matching distribution found for msvc-runtime

エラー内容からPythonバージョンも関係してそうだったため、msvc-runtimeパッケージについて、以下のサイトで確認してみました。

msvc-runtime
Install the Microsoft™ Visual C++™ runtime DLLs to the sys.prefix and Scripts directories

結果として、msvc-runtimeパッケージの最新版はcp311となっており、Python 3.11には対応していますが、Python 3.12には対応できていないようでした。このため、Python 3.11に変更して再度インストールしてみます。

>pip install msvc-runtime
Collecting msvc-runtime
  Downloading msvc_runtime-14.34.31931-cp311-cp311-win_amd64.whl.metadata (470 bytes)
Downloading msvc_runtime-14.34.31931-cp311-cp311-win_amd64.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 523.0 kB/s eta 0:00:00
Installing collected packages: msvc-runtime
Successfully installed msvc-runtime-14.34.31931

>python
Python 3.11.8 (tags/v3.11.8:db85d51, Feb  6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import wordcloud
>>> dir(wordcloud)
['ImageColorGenerator', 'STOPWORDS', 'WordCloud', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_version', 'color_from_image', 'get_single_color_func', 'query_integral_image', 'random_color_func', 'tokenization', 'wordcloud']
>>>

今度は問題なくmsvc-runtimeパッケージもインストールされ、wordcloudパッケージも利用できるようになりました。

コメント

タイトルとURLをコピーしました