mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Docs] Confusing use of top-level code in notebooks that cannot copy-paste into Mojo files

Open jordantgh opened this issue 1 year ago • 3 comments

Where is the problem?

https://docs.modular.com/mojo/programming-manual.html#python-integration

What can we do better?

The provided code chunk doesn't work:

from python import Python

# This is equivalent to Python's `import numpy as np`
let np = Python.import_module("numpy")

# Now use numpy as if writing in Python
array = np.array([1, 2, 3])
print(array)

The correct syntax seems to be from python.python import Python, or else there is some missing information to get the provided syntax to work.

Anything else?

No response

jordantgh avatar Sep 07 '23 18:09 jordantgh

First, you need to add def main(), then you need to install numpy using pip.

code will be like this

from python import Python

def main():
    # This is equivalent to Python's `import numpy as np`
    let np = Python.import_module("numpy")

    # Now use numpy as if writing in Python
    array = np.array([1, 2, 3])
    print(array)

You should install the Mojo VS Code extension, it explains many errors.

shawn-peng avatar Sep 07 '23 19:09 shawn-peng

Yes! I have discovered this, thanks. However, the documentation remains misleading as far as I can see, I guess they are "aspirational".

jordantgh avatar Sep 07 '23 19:09 jordantgh

Sorry about that, it's a bit confusing because the documentation is written in a Jupyter notebook, which allows top-level code like this. We can do better to make that more clear...

scottamain avatar Sep 07 '23 19:09 scottamain