ModularM
Modular•3y ago•
4 replies
exographicskip

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)

Getting
Ī» mojo ../hello.šŸ”„
Hello World!
Error: Python not found
Hello Mojo
Hello Mojo

What am I missing here? Thanks in advance!
Was this page helpful?