Does this error mean that this python library can not be used in Mojo?

I have this code:
from python import Python

def search_images(term, max_images=30):
let DDGS = Python.import_module("duckduckgo_search.DDGS")
let ddgs = DDGS()

print("Searching for " + term)

let ddgs_images_gen = ddgs.images(term)

def main():
search_images("cats")
from python import Python

def search_images(term, max_images=30):
let DDGS = Python.import_module("duckduckgo_search.DDGS")
let ddgs = DDGS()

print("Searching for " + term)

let ddgs_images_gen = ddgs.images(term)

def main():
search_images("cats")
and get this error:
Unhandled exception caught during execution: cannot import name 'etree' from 'lxml' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lxml/__init__.py)
mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: cannot import name 'etree' from 'lxml' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lxml/__init__.py)
mojo: error: execution exited with a non-zero result: 1
This is after running pip install duckduckgo_search Does this mean that this library currently can not work in Mojo?
9 Replies
Sardor
Sardor11mo ago
Does this work when you run in python?
irrationaljared
irrationaljared11mo ago
This code is already Mojo specific, but the python equivalent (as best I can tell) does work: https://colab.research.google.com/drive/1KdgI3Q-BFjNckUJYca_YUXMKwmznbyDr?usp=sharing
gryznar
gryznar11mo ago
Can you perform in your Python env:
from lxml import etree
from lxml import etree
?
irrationaljared
irrationaljared11mo ago
Did you mean in the mojo environment? This all works in Python and yes, in python if you call from lxml import etree it works fine
gryznar
gryznar11mo ago
Ahh, I see what's the problem I think. Via Python.import_module() you can only import modules, no variables within it. Please retry with removing .DDGS part
irrationaljared
irrationaljared11mo ago
Unfortunately this produces the same error:
from python import Python

fn search_images(term: String, max_images: UInt8 = 30):
try:
let duckduckgo_search = Python.import_module("duckduckgo_search")
let ddgs = duckduckgo_search.DDGS()

except e:
print("error:", e)

fn main():
search_images("cats")
from python import Python

fn search_images(term: String, max_images: UInt8 = 30):
try:
let duckduckgo_search = Python.import_module("duckduckgo_search")
let ddgs = duckduckgo_search.DDGS()

except e:
print("error:", e)

fn main():
search_images("cats")
Error:
error: cannot import name 'etree' from 'lxml' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lxml/__init__.py)
error: cannot import name 'etree' from 'lxml' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lxml/__init__.py)
ModularBot
ModularBot11mo ago
Congrats @irrationaljared, you just advanced to level 2!
gryznar
gryznar11mo ago
Strange. I have no clue, sorry
irrationaljared
irrationaljared11mo ago
Is it expected that this channel might provide more support or should I inquire elsewhere?
Want results from more Discord servers?
Add your server