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
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")
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
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
4 Replies
AffableHoneyBadger
@Hammad Ali Have you tried adding the absolute path? I'm also not sure you need the .py on the end of module name. So something like:
from python import Python

fn main() raises:
Python.add_to_path("/the/path/to/module/")
var module = Python.import_module("python_curl")
from python import Python

fn main() raises:
Python.add_to_path("/the/path/to/module/")
var module = Python.import_module("python_curl")
AffableHoneyBadger
Python integration | Modular Docs
Using Python and Mojo together.
Hammad Ali
Hammad AliOP5mo ago
Thanks! That worked perfectly
AffableHoneyBadger
Awesome! 👍
Want results from more Discord servers?
Add your server