pythonmodularmojolang

Mojo (Mac) using wrong python lib


As most Mac users, I have two separate versions of python on my machine: The one installed by Apple (version 3.9 in /usr/bin) and one installed via Homebrew (version 3.11 in /opt/homebrew/bin).

I've put the latter first in path and, sure enough,

which python3

prints

/opt/homebrew/bin/python3

and

python3 --version

prints

Python 3.11.6

but Mojo seems to use the other version, as packages installed via pip3 are not accessible.

The following sys_path.🔥

from python import Python
 
def main():
 
    let sys = Python.import_module("sys")
    print(sys.prefix)

prints

/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9

How do I make Mojo use the correct python lib?


Solution

  • I've found not one but two possible solutions:

    1. Manually edit ~/.modular/modular.cfg, in group mojo find python_lib and set it to the path to the python lib installed with homebrew. In my case I've had to change
    /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Python3
    

    to

    /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib
    
    1. EXPORTing MOJO_PYTHON_LIBRARY with the correct path will override the config setting for the current session. I've put
    export MOJO_PYTHON_LIBRARY="/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib"
    

    in my ~/.zshrc. Don't forget to source or restart the terminal when using the rc file.

    The second option comes in handy, if you want to test against different versions of the python lib.