-
from python import Python fn main():
In the above program, it says unable to locate requests so my question is there a mojo version for this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
There's not a Mojo version of requests yet, but I believe you can try another way. from python import Python
fn main():
try:
let requests = Python.import_module("requests")
# URL you want to send a GET request to
let url = 'https://jsonplaceholder.typicode.com/todos/1'
# Send the GET request
let response = requests.delete(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
print('Request was successful')
print('Response content:')
print(response.text)
else:
print('Request failed with status code', response.status_code)
except:
print("didn't work") I don't yet see a way to write to files built in, you may have to code that by hand. Unfortunately that's above my level. But I'm trying to learn everything I can about the language while it's new and minimal so over time I can just add to my knowledge. |
Beta Was this translation helpful? Give feedback.
-
In mojo, you can't just import python modules like in python. So this won't work import requests And MikeCase is also right that there doesn't seem to be a way in mojo yet to do any kind of mojo native File or Networking IO. You can get the I haven't tested this myself, but try this fn main():
let bi = Python.import_module("builtins")
let requests = Python.import_module("requests")
let url = 'https://jsonplaceholder.typicode.com/todos/1'
# Send the GET request
let response = requests.delete(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
print('Request was successful')
print('Response content:')
print(response.text)
else:
print(f'Request failed with status code {response.status_code}')
let save = bi.str(bi.input("Do you want to save the response?: "))
if save.lower() == "yes" or save.lower() == "y":
let saveFile = bi.open("response.json", "w")
saveFile.write(f"{response.text}")
saveFile.close() You may also get a warning about a shared python library not being found if you compile it as an executable
|
Beta Was this translation helpful? Give feedback.
There's not a Mojo version of requests yet, but I believe you can try another way.