Last active
February 23, 2022 12:28
-
-
Save umbertogriffo/0f7a6b90f56ce9d30b921da572d4b7de to your computer and use it in GitHub Desktop.
This installs Python under Rosetta and assign it to pyenv to avoid: ModuleNotFoundError: No module named '_ctypes' on M1 Apple Silicon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This installs Python under Rosetta and assign it to pyenv. | |
# This way of installing Python avoids: ModuleNotFoundError: No module named '_ctypes' | |
# pyenv has to be installed from Github https://laict.medium.com/install-python-on-macos-11-m1-apple-silicon-using-pyenv-12e0729427a9 | |
version=$1 | |
if [ "$#" -ne 1 ]; then | |
echo "Illegal number of parameters. Usage:" | |
echo "./install_python_rosetta.sh <python-version>" | |
exit 1 | |
fi | |
command -v pyenv >/dev/null 2>&1 || { echo >&2 "Pyenv is required but it's not installed. Aborting."; exit 1; } | |
command -v arch -x86_64 /usr/local/homebrew/bin/brew >/dev/null 2>&1 || { echo >&2 "arch -x86_64 is required but it's not installed. Aborting."; exit 1; } | |
alias ibrew='arch -x86_64 /usr/local/homebrew/bin/brew' | |
major=$(echo "$version" | cut -d. -f1) | |
minor=$(echo "$version" | cut -d. -f2) | |
micro=$(echo "$version" | cut -d. -f3) | |
ibrew install python@"${major}.${minor}" | |
if [ $? -ne 0 ] | |
then | |
exit 255 | |
fi | |
# Add `python` executable (symlink to `python3`) | |
ln -s python3 "$(ibrew --prefix python@"${major}.${minor}")"/bin/python | |
# Symlink x86 Python into pyenv | |
ln -s "$(ibrew --prefix python@"${major}.${minor}")" "$HOME"/.pyenv/versions/"${major}.${minor}.${micro}" | |
# Check | |
pyenv local "${major}.${minor}.${micro}" | |
python -V | |
python -c 'import _ctypes' # works! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment