'object' does not refer to a package or module

69 views Asked by At

I tried the below in Jupiter, and it is working fine,

enter image description here

But one I tried to use at at file.mojo as:

from python import Python

# The Python way: `import requests as http`
let http = Python.import_module("requests")

# Now use numpy as if writing in Python

fn main():
    let url = 'http://google.com'
    let response = http.get(url)
    let content = response.text
    print(content)

I got the errors:

/home/hajsf/mojo/python.mojo:1:20: error: recursive reference to declaration
from python import Python
                   ^
/home/hajsf/mojo/python.mojo:1:6: note: previously used here
from python import Python
     ^
/home/hajsf/mojo/python.mojo:1:1: error: 'object' does not refer to a package or module
from python import Python
^
mojo: error: failed to parse the provided Mojo
1

There are 1 answers

0
Iliya On BEST ANSWER

The recursive reference to declaration comes from your file being called python.mojo, if you rename it and fix this you will get cannot call function that may raise in a context that cannot raise, to fix this one the import of the module has to be inside a function that can catch exceptions, like:

from python import Python

fn main() raises:
    let http = Python.import_module("requests")

    let url = "http://google.com"
    let response = http.get(url)
    let content = response.text
    print(content)

the raises after main() is the important part, point 4 from here.