Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy import error (in CLI, Jupyter and REPL) #2636

Closed
CalmScout opened this issue May 12, 2024 · 2 comments
Closed

Numpy import error (in CLI, Jupyter and REPL) #2636

CalmScout opened this issue May 12, 2024 · 2 comments
Labels
bug Something isn't working mojo-python-interop mojo-repo Tag all issues with this label

Comments

@CalmScout
Copy link

CalmScout commented May 12, 2024

Bug description

Dear members of Mojo community,

When I run the following code:

from python import Python
 
def main():
    var np = Python.import_module("numpy")
    var array = np.array([1, 2, 3])
    print(array)

The following error appears:

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

The behaviour persist if I run the script via Terminal or in Jupyter Notebook or enter the code in Mojo REPL.
The only version of Mojo on the system is one with MAX Engine. The only python environment that has mojo has installed numpy:

❯ mamba list | grep numpy
numpy 1.26.4 pypi_0 pypi

This is a conde env inside pyenv env. Hello world examples, that requires only Mojo (no Python imports) work correctly.

I appreciate any help on the issue.

Best regards,
Anton.

Steps to reproduce

Code snippet is provided in the description above.

System information

❯ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 24.04 LTS
Release:	24.04
Codename:	noble
❯ mojo -v
mojo 24.3.0 (9882e19d)
❯ modular -v
modular 0.8.0 (39a426b5)
@CalmScout CalmScout added bug Something isn't working mojo-repo Tag all issues with this label labels May 12, 2024
@jackos
Copy link
Collaborator

jackos commented May 13, 2024

Hi @CalmScout try this after activating your conda environment to link Mojo to it:

export MOJO_PYTHON_LIBRARY="$(find $CONDA_PREFIX/lib -iname 'libpython*.[s,d]*' | sort -r | head -n 1)"
echo "export MOJO_PYTHON_LIBRARY=$MOJO_PYTHON_LIBRARY" >> ~/.zshrc
echo "export MOJO_PYTHON_LIBRARY=$MOJO_PYTHON_LIBRARY" >> ~/.bashrc

Then restart your shell and try running mojo again

@CalmScout
Copy link
Author

Hi @jackos ,

Thank you for such a prompt reply. I followed your instructions and this solved the issue! Thank you for providing instructions for both bash and zsh.

Best regards,
Anton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-python-interop mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants