Skip to content

Commit

Permalink
Fix Python interop (modular#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackos authored Aug 25, 2023
1 parent 5d65c38 commit a9dfe70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
13 changes: 5 additions & 8 deletions examples/hello_interop.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ def main():
print("Hello Mojo 🔥!")
for x in range(9, 0, -3):
print(x)
alias test_dir = "/root/mojo-examples"
try:
Python.add_to_path(test_dir)
let test_module: PythonObject = Python.import_module("simple_interop")
if test_module:
_ = test_module.test_interop_func()
else:
print("Could not locate module: ", test_module)
Python.add_to_path(".")
Python.add_to_path("./examples")
let test_module = Python.import_module("simple_interop")
test_module.test_interop_func()
except e:
print(e.value)
pass
print("could not find module simple_interop")
6 changes: 3 additions & 3 deletions examples/matmul.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ struct Matrix:
fn run_matmul_python(M: Int, N: Int, K: Int) -> Float64:
var gflops: Float64 = 0.0
let python = Python()
alias test_dir = "/root/mojo-examples"
try:
Python.add_to_path(test_dir)
Python.add_to_path(".")
Python.add_to_path("./examples")
let pymatmul_module: PythonObject = Python.import_module("pymatmul")
if pymatmul_module:
gflops = pymatmul_module.benchmark_matmul_python(
M, N, K
).to_float64()
else:
print("Python matmul module not found")
print("pymatmul module not found")
except e:
print(e.value)
pass
Expand Down
8 changes: 8 additions & 0 deletions examples/pymatmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

# Simple program demonstrating a naive matrix multiplication in Python

import importlib
import sys
import subprocess

if not importlib.find_loader("numpy"):
print("Numpy not found, installing...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])

import numpy as np
from timeit import timeit

Expand Down
8 changes: 8 additions & 0 deletions examples/simple_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

# Simple python program to test interop

import importlib
import sys
import subprocess

if not importlib.find_loader("numpy"):
print("Numpy not found, installing...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])

import numpy as np
from timeit import timeit

Expand Down

0 comments on commit a9dfe70

Please sign in to comment.