How can I resolve proxy when using API from python?

Hi,
I have created my API key for Materials Project.
However, when I tried to run the following

from mp_api.client import MPRester
with MPRester(“INSERTED_API_KEY”) as mp:
structure = mp.get_structure_by_material_id(“mp-1”)
print(structure)

this is stopped around urllib3.

I would really appreciate any help!

Please post the full error message. Thanks!

Thank you very much @tschaume !

Here is the messages when I interrupted the command.
I would appreciate any help. Thank you:)

File “test.py”, line 22, in
structure = mp.get_structure_by_material_id(“mp-1”)
File “/opt/conda/lib/python3.7/site-packages/mp_api/client.py”, line 205, in get_structure_by_material_id
material_id=material_id, final=final
File “/opt/conda/lib/python3.7/site-packages/mp_api/routes/materials/client.py”, line 37, in get_structure_by_material_id
response = self.get_data_by_id(material_id, fields=[“structure”])
File “/opt/conda/lib/python3.7/site-packages/mp_api/core/client.py”, line 371, in get_data_by_id
criteria=criteria, fields=fields, suburl=document_id, # type: ignore
File “/opt/conda/lib/python3.7/site-packages/mp_api/core/client.py”, line 336, in _query_resource_data
use_document_model=use_document_model,
File “/opt/conda/lib/python3.7/site-packages/mp_api/core/client.py”, line 271, in _query_resource
response = self.session.get(url, verify=True, params=criteria)
File “/opt/conda/lib/python3.7/site-packages/requests/sessions.py”, line 600, in get
return self.request(“GET”, url, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/requests/sessions.py”, line 587, in request
resp = self.send(prep, **send_kwargs)
File “/opt/conda/lib/python3.7/site-packages/requests/sessions.py”, line 701, in send
r = adapter.send(request, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/requests/adapters.py”, line 499, in send
timeout=timeout,
File “/opt/conda/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 710, in urlopen
chunked=chunked,
File “/opt/conda/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 386, in _make_request
self._validate_conn(conn)
File “/opt/conda/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 1040, in _validate_conn
conn.connect()
File “/opt/conda/lib/python3.7/site-packages/urllib3/connection.py”, line 358, in connect
conn = self._new_conn()
File “/opt/conda/lib/python3.7/site-packages/urllib3/connection.py”, line 175, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File “/opt/conda/lib/python3.7/site-packages/urllib3/util/connection.py”, line 85, in create_connection
sock.connect(sa)
KeyboardInterrupt

Try upgrading to python 3.9 at least and then also make sure to run the latest versions of pymatgen and mp-api.

1 Like

Hi @tschaume
Thank you for your kind reply.
I will try python 3.9.

Hi @tschaume

The script worked!
Thank you so much for your support! :slight_smile:

1 Like