代码:

from python import Python



fn main():
# fn main() raises:
    # This is equivalent to Python's `import numpy as np`
    let np = Python.import_module("numpy")
    let a = np.array([1, 2, 3])
    print(a)

 

运行报错:

[02:19:48](mojo) devil@OMEN:~/mojo_workspaces$ /home/devil/.modular/pkg/packages.modular.com_mojo/bin/mojo /home/devil/mojo_workspaces/x.mojo
/home/devil/mojo_workspaces/x.mojo:8:34: error: cannot call function that may raise in a context that cannot raise
let np = Python.import_module("numpy")
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/home/devil/mojo_workspaces/x.mojo:8:34: note: try surrounding the call in a 'try' block
let np = Python.import_module("numpy")
^
/home/devil/mojo_workspaces/x.mojo:5:1: note: or mark surrounding function as 'raises'
fn main():
^
/home/devil/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to parse the provided Mojo

 

 

编程语言mojo报错:error: cannot call function that may raise in a context that cannot raise_Python

 

 

================================================

 

 

mojo是一个新的AI编程语言,其中一个特点就是可以很好的和python融合,上面的代码则是在mojo中调用python,报错,解决方案:

链接:

https://github.com/modularml/mojo/issues/762

 

编程语言mojo报错:error: cannot call function that may raise in a context that cannot raise_python_02

 

 

 

 这里修改后的代码:

from python import Python



fn main() raises:
    # This is equivalent to Python's `import numpy as np`
    let np = Python.import_module("numpy")
    let a = np.array([1, 2, 3])
    print(a)

 

 

运行:

编程语言mojo报错:error: cannot call function that may raise in a context that cannot raise_github_03

 

 

================================================