Interoperability of Mojo Programming Language with Python Ecosystem: Programming in Mojo — Part V
Mojo Programming Language combines the usability of Python but, with the performance of C, C++ and Rust. Mojo Programming Language has advantages like Usability & Programmability, best performance, Interoperability and Extensibility. Mojo Programming Language is being developed and introduced by Modular.
In this Article/Blog, we are going to see the Interoperability of Mojo Programming Language with Python Ecosystem to import Python modules / libraries / custom code / local modules and use it with Mojo Programming and also to create Python types from Mojo types.
In this Blog, Programming in Mojo is done using Mojo Playground.
Interoperability of Mojo Programming Language with Python Ecosystem:
In Mojo Programming Language, Interoperability with Python Ecosystem can be done to import Python modules / libraries / custom code / local modules and use it with Mojo Programming and also to create Python types from Mojo types. This mechanism uses CPython Interpreter to run the Python code and currently it works with all Python modules.
I. To import Python modules / libraries and use it with Mojo Programming:
To import a Python Module / Library in Mojo, first we should import Python from python Module / Library, then we should use Python.import_module() fucntion with the required module name.
Code Explanation:
from python import Python
python is a module / library from which Python is imported which provides the methods that help us to use Python code in Mojo.
Then we use Python.import_module() function to import numpy Python module and then now we can use numpy as if writing in Python and we do the required operations with the imported module.
Likewise, we can import any other Python modules / libraries. At Present, we cannot import individual members like a single Python class or function but we must import the whole Python module and then access the members through the module name.
II. To import Python custom code / local modules and use it with Mojo Programming:
If we have some local Python code / custom code / local modules to use in Mojo Programming, we should add the directory to Python path using Python.add_to_path() function and then import the module.
For example, we have a Python File named program.py,
In Mojo file / in Mojo Programming, we can import and use the local Python code / custom code / local modules as shown below,
We no need to worry about memory management while using Python in Mojo Programming. Everything just works because Mojo Programming Language was designed for Python from the beginning.
III. Mojo types in Python:
Mojo Primitive types implicitly convert to Python Objects, currently they support integers, floats, Booleans, strings, lists and tuples.
Example:
The following python function prints the Python types,
Then, we can pass the Python function Mojo types with no problem as follows,
Note that in a Jupyter Notebook, the Python Function declared above is automatically available to any Mojo code in following code cells.
Mojo doesn’t have a standard Dictionary till now, so it is not yet possible to create a Python Dictionary from Mojo Dictionary. But we can work with Python Dictionaries in Mojo. We can use dict() method to create a Python Dictionary in Mojo.
Since, Mojo Programming Language is still in development some features of Python Programming Language are not yet implemented in Mojo and not a full superset of Python yet, but all the missing Python features will be implemented soon as mentioned by Modular-Mojo Development Team. But Mojo Programming Language already consists of many features and capabilities beyond Python Programming Language.
We had seen about Interoperability of Mojo Programming Language with Python Ecosystem. Programming in Mojo and Mojo Programming concepts will be discussed further.
Thankyou for reading this blog on Interoperability of Mojo Programming Language with Python Ecosystem: Programming in Mojo — Part V.
Reference: