api
api copied to clipboard
`MP_API_KEY` is retrieved at import time
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
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 😄