M
Modular15mo ago
zack

Support for Python requests?

Is the Python requests package supported natively? If so how do I import it and use it?
6 Replies
zack
zackOP15mo ago
Ahh ok, must resolve the package at runtime as it is a python package with a python C backend, it would be interesting to see mojo’s own implantation of networking
kyleb1328
kyleb132815mo ago
im interested in that too. wondering if its on the roadmap
Jack Clayton
Jack Clayton15mo ago
Yep it works, if you're getting an error you might have to add where requests is located to your path, e.g. I created a venv here:
python -m venv .env
.env/bin/activate
pip install requests
python -m venv .env
.env/bin/activate
pip install requests
Then you can run this:
from python import Python


fn main() raises:
Python.add_to_path(".env/lib/python3.11/site-packages")
let requests = Python.import_module("requests")
let response = requests.get("https://google.com")
print(response.text)
from python import Python


fn main() raises:
Python.add_to_path(".env/lib/python3.11/site-packages")
let requests = Python.import_module("requests")
let response = requests.get("https://google.com")
print(response.text)
kyleb1328
kyleb132815mo ago
this should work as well:
from python import Python

fn main():
try:
let req = Python.import_module("requests")
let response = req.request("GET", "http://xyz:123/abc")
print(response.text)
except ImportError:
print("Please install requests module")
from python import Python

fn main():
try:
let req = Python.import_module("requests")
let response = req.request("GET", "http://xyz:123/abc")
print(response.text)
except ImportError:
print("Please install requests module")
zack
zackOP15mo ago
Ok, thank you all for the help
Want results from more Discord servers?
Add your server