0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python 3.13でcgiモジュールが削除されたので、googletransの互換性を直して画像生成aiを正しく実行させました。

Posted at

初めに

※この記事では、cgiモジュールをインストールすることはしません。
予め、ご了承ください。m(_ _)m

私の環境は、
mac OS Sequoia15.6
Python 3.13.5
pip 3.13
(インストール方法、Pythonのサイトから直接ダウンロードした際、
pipが存在しないとエラーが出たため、home brueからインストールし直しました。)

ソース素はこちらのサイトと、ClaudeというAIが出した結果を元にしております。
https://chaba-lab.com/python-3-13-cgi-remove-20241220/

実行した画像生成AI(Claudeでソースに手を加えて実行しました。)
https://qiita.com/m_wtnb/items/00e5db63cde2bb056c27

この記事が少しでも参考になれば幸いです。
皆様のプログラミングを少しでも支えることができればと思い、書かせていただきました。これからも頑張ってお仕事や趣味などをされて下さい。

本題

https://chaba-lab.com/python-3-13-cgi-remove-20241220/
にも書かれていたとおり、Python 3.13.5のPythonではcgiモジュールが削除されていました。
なので、画像生成AIが動作せず私は困っていました。
初めは、なんでかな?googletransのdiffusersが入っていないのかな?と
思い、インストールを再度実行していました。
けれど問題は一向に解決せず、cgiモジュールが存在しないみたいなエラーが出ました。その内容はこちらです。

Traceback (most recent call last):
  File "/Users/hayatotokushige/Desktop/プログラミング/main.py", line 3, in <module>
    from googletrans import Translator
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/googletrans/__init__.py", line 6, in <module>
    from googletrans.client import Translator
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/googletrans/client.py", line 13, in <module>
    import httpx
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/httpx/__init__.py", line 2, in <module>
    from ._api import delete, get, head, options, patch, post, put, request, stream
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/httpx/_api.py", line 3, in <module>
    from ._client import Client, StreamContextManager
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/httpx/_client.py", line 8, in <module>
    from ._auth import Auth, BasicAuth, FunctionAuth
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/httpx/_auth.py", line 10, in <module>
    from ._models import Request, Response
  File "/Users/hayatotokushige/my_project/env/lib/python3.13/site-packages/httpx/_models.py", line 1, in <module>
    import cgi
ModuleNotFoundError: No module named 'cgi'

私の解釈では、ファイルのパスの向こうにあるソースが間違っているか、
存在しないからエラーを吐いているのだと思いました。
これは私のプログラミングの経験則とJavaなどのプログラムがこのようなエラーを吐いた時の体験を元にしてそう思いました。

解決方法

方法1: googletransの代替バージョンを使用
←私はこの方法で解決しました。

pip uninstall googletrans
pip install googletrans==3.1.0a0

こちらをインストールしたのちに、

def parse_arguments() -> argparse.Namespace:

のように、ラムダ型に画像生成AIのソースを直すのと、

import requests
import json

を追記するだけでした。
私はこの方法でcgiモジュールのエラーが治りました。

私は実践しておりませんが、
以下に、他の方法もあるので記述しておきます。

方法2: deep-translatorを使用

pip install deep-translator

をインストールし、

from diffusers import DiffusionPipeline

の後に、

from deep_translator import GoogleTranslator

を追記するです。
これだけで解決するらしいです。

方法3: Pythonのバージョンを下げる
Python 3.12以下を使用する場合:

# condaを使用している場合
conda install python=3.12
pip install googletrans==4.0.0rc1

# pyenvを使用している場合
pyenv install 3.12.0
pyenv local 3.12.0

これは仮想環境の話をしていますね。
pyenvかcondaどちらを使用して、ソースを自分が書いているかということかと思います。

最後の方法がこちらです。
より安定した解決方法が必要な場合は、deep-translatorを使用してください:

pip install deep-translator diffusers transformers torch torchvision accelerate

こちらをインストールするという方法です。
googletransの代わりにdeep-translatorのdiffuersを使用するのでしょうね。

私の考察と、解決方法を記したこの記事はこれにて終了です。
最後まで、ご閲覧いただきありがとうございました。m(_ _)m

皆様のこれからのご活躍をご検討しております。
プログラムライフも応援しております。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?