exographicskip
exographicskip
MModular
Created by exographicskip on 1/6/2024 in #questions
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!
5 replies