api icon indicating copy to clipboard operation
api copied to clipboard

`MP_API_KEY` is retrieved at import time

Open DanielYang59 opened this issue 1 year ago • 1 comments

MP_API_KEY is retrieved at import time, perhaps considering getting at init time instead https://github.com/janosh/pymatviz/pull/207#issuecomment-2379211936? https://github.com/materialsproject/api/blob/2c89d4c015940daa5c28b20d4a5d757b8ab6b29f/mp_api/client/mprester.py#L72

If someone want to set the environment variable at runtime (assume no MP_API_KEY env var is set beforehand), it would fail:

import os
from mp_api.client import MPRester  # >>> os.environ.get("MP_API_KEY", None) being execuated


os.environ["MP_API_KEY"] = "KEY"

mp_api = os.environ.get("MP_API_KEY")
assert mp_api is not None  # >>> passes

struct = MPRester().get_structure_by_material_id("mp-19017")  # >>> fails as MP_API_KEY is None

DanielYang59 avatar Sep 27 '24 13:09 DanielYang59

That's true. The only way to set the API key at runtime right now is explicitly setting the argument in MPRester(), i.e.

struct = MPRester(mp_api).get_structure_by_material_id("mp-19017")

We happily accept PRs that maintain backward compatibility though 😄

tschaume avatar Sep 27 '24 16:09 tschaume