Python.import_module not working as expected

Hi there ❤️‍🔥 ! Trying to go through a basic hello world example and would like to import stdlib modules from python. My silly app looks like this:
#!/usr/bin/env mojo

from python import Python

fn name() -> String:
try:
let input = Python.import_module('input')
return str(input("What is your name? "))
except:
print("Error: Python not found")
return "Mojo"

fn main() -> None:
# let is an immutable variable
let hello = "Hello World!"
print(hello)

# var is a mutable variable
var name = name()
print("Hello", name)

# mutate the variable
name = "Mojo"
print("Hello", name)
#!/usr/bin/env mojo

from python import Python

fn name() -> String:
try:
let input = Python.import_module('input')
return str(input("What is your name? "))
except:
print("Error: Python not found")
return "Mojo"

fn main() -> None:
# let is an immutable variable
let hello = "Hello World!"
print(hello)

# var is a mutable variable
var name = name()
print("Hello", name)

# mutate the variable
name = "Mojo"
print("Hello", name)
Getting
λ mojo ../hello.🔥
Hello World!
Error: Python not found
Hello Mojo
Hello Mojo
λ mojo ../hello.🔥
Hello World!
Error: Python not found
Hello Mojo
Hello Mojo
What am I missing here? Thanks in advance!
3 Replies
exographicskip
exographicskip9mo ago
Debug console shows
Running initCommands:
(lldb) settings set target.show-hex-variable-values-with-leading-zeroes false
(lldb) settings set target.process.optimization-warnings false
(lldb) plugin load '/Users/lance/.modular/pkg/packages.modular.com_mojo/lib/libMojoLLDB.dylib'
(lldb) command script import /Users/lance/.modular/pkg/packages.modular.com_mojo/lib/lldb-visualizers/lldbDataFormatters.py
error: module importing failed: This script interpreter does not support importing modules.
(lldb) command script import /Users/lance/.modular/pkg/packages.modular.com_mojo/lib/lldb-visualizers/mlirDataFormatters.py
error: module importing failed: This script interpreter does not support importing modules.
Hello World!
Error: Python not found
Hello Mojo
Hello Mojo
Running initCommands:
(lldb) settings set target.show-hex-variable-values-with-leading-zeroes false
(lldb) settings set target.process.optimization-warnings false
(lldb) plugin load '/Users/lance/.modular/pkg/packages.modular.com_mojo/lib/libMojoLLDB.dylib'
(lldb) command script import /Users/lance/.modular/pkg/packages.modular.com_mojo/lib/lldb-visualizers/lldbDataFormatters.py
error: module importing failed: This script interpreter does not support importing modules.
(lldb) command script import /Users/lance/.modular/pkg/packages.modular.com_mojo/lib/lldb-visualizers/mlirDataFormatters.py
error: module importing failed: This script interpreter does not support importing modules.
Hello World!
Error: Python not found
Hello Mojo
Hello Mojo
Compiled version
λ ./hello
Hello World!
Mojo/Python interoperability error: Unable to locate a suitable libpython, please set `MOJO_PYTHON_LIBRARY`
Segmentation fault: 11
λ ./hello
Hello World!
Mojo/Python interoperability error: Unable to locate a suitable libpython, please set `MOJO_PYTHON_LIBRARY`
Segmentation fault: 11
Which is interesting bc I set
export MOJO_PYTHON_LIBRARY=/Users/lance/.asdf/installs/python/3.11.6/lib/libpython3.11.dylib
export MOJO_PYTHON_LIBRARY=/Users/lance/.asdf/installs/python/3.11.6/lib/libpython3.11.dylib
zverianskii
zverianskii9mo ago
input is not a module, try this one:
let builtins = Python.import_module('builtins')
return str(builtins.input("What is your name? "))
let builtins = Python.import_module('builtins')
return str(builtins.input("What is your name? "))
exographicskip
exographicskip9mo ago
That did it! Thank you! This is what I landed on fwiw
#!/usr/bin/env mojo

fn main():
from python import Python

# let is an immutable variable
let hello = "Hello World!"
print(hello)

# var is a mutable variable
try:
let builtins = Python.import_module("builtins")
var name = str(builtins.input("What is your name? "))
print("Hello", name)
name = "World" # mutate the variable
print("Hello", name)
except:
print("Unable to import Python module 'builtins'")
#!/usr/bin/env mojo

fn main():
from python import Python

# let is an immutable variable
let hello = "Hello World!"
print(hello)

# var is a mutable variable
try:
let builtins = Python.import_module("builtins")
var name = str(builtins.input("What is your name? "))
print("Hello", name)
name = "World" # mutate the variable
print("Hello", name)
except:
print("Unable to import Python module 'builtins'")
Want results from more Discord servers?
Add your server