VPS の移行を実施中に発生した Python エラーと対処法についてまとめます。
前提
移行前と移行後の OS Version 、 Python の Version について記載します。
-
OS Version
- 移行前
cat /etc/redhat-release CentOS release 6.9 (Final)
- 移行後
cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
- 移行前
-
Python の Version
-
移行前
python2.7 -V Python 2.7.8
-
移行後
python3.6 -V Python 3.6.4
-
発生した Python エラー
ModuleNotFoundError: No module named ‘memcache’
-
原因
memcache モジュールがない。
python-memcached
をインストールしていなかった。 -
対処法
python-memcached
をインストールした。
python3.6 -m pip install python-memcached
-
参考
python - Importerror: No module named memcache (Django project) - Stack Overflow
No module named ‘StringIO’
-
原因
Python 2 と、 Python 3 で、StringIO
のパッケージが変更されており、Python 3 では import に失敗していた。 -
対処法
import 箇所の実装を以下のように修正した。
try: from StringIO import StringIO except ImportError: from io import StringIO
django.template.exceptions.TemplateSyntaxError: ‘compress’ is not a registered tag library
-
原因
django-compressor
をインストールしていなかった。 -
対処法
django-compressor
をインストールした。python3.6 -m pip install django-compressor
Penthouse 実行時に error while loading shared libraries: libfontconfig.so.1 が発生
これは、Python ではなく、Python から呼び出している Penthouse でエラーが発生していました。
-
原因
fontconfig-devel
というライブラリがインストールされていなかった。 -
対処法
fontconfig-devel
をインストール。
yum -y install fontconfig-devel
TypeError: Unicode-objects must be encoded before hashing
-
原因
hashlib というライブラリでエラー、Python 2 では 動作するが、 Python 3 では hash 化する前に、エンコーディングをする必要がある。 -
対処法
対象箇所の実装を以下のように修正した。
import six h = hashlib.sha1() if six.PY2: h.update(critical_css_fragment) else: h.update(critical_css_fragment.encode("utf-8"))
cssmin.py で、TypeError: a bytes-like object is required, not ‘str’ が発生
zacharyvoase/cssmin: NO LONGER MAINTAINED. A Python port of the YUI CSS compression algorithm. でエラーが発生していました。
-
原因
cssmin.py の Python 3 の互換性の問題かと思いましたが、本体ではなく、使用している側で decode 指定がないのが原因でした。 -
対処法
cssmin.py の 使用箇所を修正し、Python 3 の場合、decode 処理を行うようにしました。
import six if six.PY2: critical_css = cssmin(critical_css) else: critical_css = cssmin(critical_css.decode('utf-8'))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
-
原因
MongoDB をインストールしていない。 -
対処法
MongoDB をインストールした。
ModuleNotFoundError: No module named ‘HTMLParser’
-
原因
Python2 > Python3 で HTMLParser のパッケージが変更になった。 -
対処法
HTMLParser の import 記述を以下のように修正した。
try: import HTMLParser except ImportError: from html.parser import HTMLParser
以上です。
コメント