Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP_API_KEY is retrieved at import time #935

Closed
DanielYang59 opened this issue Sep 27, 2024 · 1 comment · Fixed by #936
Closed

MP_API_KEY is retrieved at import time #935

DanielYang59 opened this issue Sep 27, 2024 · 1 comment · Fixed by #936

Comments

@DanielYang59
Copy link
Contributor

DanielYang59 commented Sep 27, 2024

MP_API_KEY is retrieved at import time, perhaps considering getting at init time instead janosh/pymatviz#207 (comment)?

DEFAULT_API_KEY = os.environ.get("MP_API_KEY", None)

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
@tschaume
Copy link
Member

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants