■本ページの目的
2025.03.01ごろより、MaterialsProjectのDBに更新が入り、python3.9環境からAPIアクセスの際にエラーが発生する事象が確認されています。
エラーに対する対応方法を整理しましたので紹介します。
-----------------------------
目次
■対処方法①(最新版のmp-api, pymatgenへのアップデート)
-----------------------------
■発生するエラーの詳細
エラーが発生したライブラリの組み合わせとエラー内容を共有します。
| ライブラリ | バージョン |
| python | 3.9.16 |
| pymatgen | 2024.7.18 |
| mp-api | 0.43.0 |
| emmet-core | 0.84.2 |
エラーコード例
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~/.py39/lib/python3.9/site-packages/mp_api/client/mprester.py:252, in MPRester.__init__(self, api_key, endpoint, notify_db_version, include_user_agent, monty_decode, use_document_model, session, headers, mute_progress_bars)
249 # Instantiate top level molecules and materials resters and set them as attributes
250 core_suffix = ["molecules/core", "materials/core"]
--> 252 core_resters = {
253 cls.suffix.split("/")[0]: cls(
254 api_key=api_key,
255 endpoint=self.endpoint,
256 include_user_agent=include_user_agent,
257 session=self.session,
258 monty_decode=monty_decode,
259 use_document_model=use_document_model,
260 headers=self.headers,
261 mute_progress_bars=self.mute_progress_bars,
262 )
263 for cls in self._all_resters
264 if cls.suffix in core_suffix
265 }
267 # Set remaining top level resters, or get an attribute-class name mapping
268 # for all sub-resters
269 _sub_rester_suffix_map = {"materials": {}, "molecules": {}}
File ~/.py39/lib/python3.9/site-packages/mp_api/client/mprester.py:253, in <dictcomp>(.0)
249 # Instantiate top level molecules and materials resters and set them as attributes
250 core_suffix = ["molecules/core", "materials/core"]
252 core_resters = {
--> 253 cls.suffix.split("/")[0]: cls(
254 api_key=api_key,
255 endpoint=self.endpoint,
256 include_user_agent=include_user_agent,
257 session=self.session,
258 monty_decode=monty_decode,
259 use_document_model=use_document_model,
260 headers=self.headers,
261 mute_progress_bars=self.mute_progress_bars,
262 )
263 for cls in self._all_resters
264 if cls.suffix in core_suffix
265 }
267 # Set remaining top level resters, or get an attribute-class name mapping
268 # for all sub-resters
269 _sub_rester_suffix_map = {"materials": {}, "molecules": {}}
File ~/.py39/lib/python3.9/site-packages/mp_api/client/core/client.py:124, in BaseRester.__init__(self, api_key, endpoint, include_user_agent, session, s3_client, debug, monty_decode, use_document_model, timeout, headers, mute_progress_bars)
122 self.headers = headers or {}
123 self.mute_progress_bars = mute_progress_bars
--> 124 self.db_version = BaseRester._get_database_version(self.endpoint)
126 if self.suffix:
127 self.endpoint = urljoin(self.endpoint, self.suffix)
File ~/.py39/lib/python3.9/site-packages/mp_api/client/core/client.py:227, in BaseRester._get_database_version(endpoint)
225 date_str = requests.get(url=endpoint + "heartbeat").json()["db_version"]
226 # Convert the string to a datetime object
--> 227 date_obj = datetime.strptime(date_str, "%Y.%m.%d")
229 # Format the datetime object as a string
230 formatted_date = date_obj.strftime("%Y.%m.%d")
File /usr/local/pyenv/versions/3.9.16/lib/python3.9/_strptime.py:568, in _strptime_datetime(cls, data_string, format)
565 def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
566 """Return a class cls instance based on the input string and the
567 format string."""
--> 568 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
569 tzname, gmtoff = tt[-2:]
570 args = tt[:6] + (fraction,)
File /usr/local/pyenv/versions/3.9.16/lib/python3.9/_strptime.py:352, in _strptime(data_string, format)
349 raise ValueError("time data %r does not match format %r" %
350 (data_string, format))
351 if len(data_string) != found.end():
--> 352 raise ValueError("unconverted data remains: %s" %
353 data_string[found.end():])
355 iso_year = year = None
356 month = day = 1
ValueError: unconverted data remains: .post
■対処方法①(最新版のmp-api, pymatgenへのアップデート)
1, mp-api, pymatgenを最新版にアップデートしてください
pip install -U mp-api pymatgen
2, emmet-coreを0.84.2にダウングレードしてください
pip uninstall emmet-core
pip install emmet-core==0.84.2
本対応方法では、python3.9環境で利用できる最新のライブラリを利用することで、エラーを回避します。
但し、現状最新のmp-api=0.45.3はemmet-core>=0.84.3rc6を要求しているため、使えない機能が見つかる場合があります。
■対処方法②(アクセス先のエンドポイントを指定)
1,MPResterを利用する際に、以下のコードを追加して、アクセス先のDBを指定してください
from mp_api.client import MPRester
# A workaround
from mp_api.client.core import BaseRester
setattr(BaseRester, "_get_database_version", lambda _endpoint: "2025.02.12.post")
with MPRester("your_api_key_here") as mpr:
# do stuff with mpr...
本対処方法では、アクセス先のエンドポイントを指定することで、エラーを回避します。
但し、利用のたびに毎回上記のコードを利用して回避する必要がありますし、エンドポイントがいつまで維持されているかは不明のため、突然使えなくなる可能性があります。