ModularM
Modular2y ago
5 replies
Hammad Ali

Error while importing local python file

Hi I was doing some experiements with python and mojo but when I tried to import the local file I got the following error:
Unhandled exception caught during execution: No module named 'python_curl'
mojo: error: execution exited with a non-zero result: 1

Here is the code:
from python import Python

fn main() raises:
    Python.add_to_path(".")
    var module = Python.import_module("python_curl.py")


Python file (python_curl.py)
import pycurl
from io import BytesIO

def curl_request():
    # Create a buffer to store the response
    buffer = BytesIO()
    
    # Create a cURL object
    c = pycurl.Curl()
    
    # Set the URL to fetch
    c.setopt(c.URL, 'https://httpbin.org/')
    
    # Write the response data to the buffer
    c.setopt(c.WRITEDATA, buffer)
    
    # Perform the request
    c.perform()
    
    # Close the cURL object
    c.close()
    
    # Get the response content
    response_body = buffer.getvalue().decode('utf-8')
    
    # Print the response
    return response_body
Was this page helpful?