mojo
mojo copied to clipboard
[Docs] Confusing use of top-level code in notebooks that cannot copy-paste into Mojo files
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
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.
Yes! I have discovered this, thanks. However, the documentation remains misleading as far as I can see, I guess they are "aspirational".
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...