Numpy: Import from source dir error

I am trying to import Numpy in my mojo project using
from python import Python

fn main() raises:
var np = Python.import_module("numpy")
from python import Python

fn main() raises:
var np = Python.import_module("numpy")
But I get the following Error:
Unhandled exception caught during execution: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
mojo: error: execution exited with a non-zero result: 1
4 Replies
Ehsan M. Kermani (Modular)
What mojo version are you using? Please make sure that numpy is visible and is in Python path which you can investigate using sys.path
pascalpolygon
pascalpolygonOP9mo ago
Hi Ehsan, thanks for the reply! I am using mojo 24.1.0 on Mac Here is my sys.path. I see site-packages so shouldn't it have access to numpy ?
['', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python310.zip', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/lib-dynload', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/site-packages']
['', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python310.zip', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/lib-dynload', '/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/site-packages']
I also tried this:
fn main() raises:
var sys = Python.import_module("sys")
_ = sys.path.append("/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/site-packages/numpy")
var np = Python.import_module("numpy")
fn main() raises:
var sys = Python.import_module("sys")
_ = sys.path.append("/Users/pascaldao/.pyenv/versions/3.10.0/lib/python3.10/site-packages/numpy")
var np = Python.import_module("numpy")
But I get the same error.
Ehsan M. Kermani (Modular)
Thanks! This can be fixed by using conda for example like https://github.com/modularml/mojo/issues/1085#issuecomment-1771403719
GitHub
[BUG]: Importing the numpy C-extensions failed. · Issue #1085 · mod...
Bug description Running the command mojo matmul.mojo results in the following error output: $ mojo matmul.mojo Unhandled exception caught during execution: IMPORTANT: PLEASE READ THIS FOR ADVICE ON...
pascalpolygon
pascalpolygonOP9mo ago
Awesome, thanks!
Want results from more Discord servers?
Add your server