'Python 2 and pyenv: build with shared libraries on macOS

I'm trying to install Python 2 with pyenv and shared libraries on macOS 12.3.1 with the command:

export PYTHON_CONFIGURE_OPTS="--enable-shared" && pyenv install 2.7.18

But I get the error:

python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Installing Python-2.7.18...
patching file configure
patching file configure.ac
patching file setup.py
patching file Mac/Tools/pythonw.c
patching file setup.py
patching file Doc/library/ctypes.rst
patching file Lib/test/test_str.py
patching file Lib/test/test_unicode.py
patching file Modules/_ctypes/_ctypes.c
patching file Modules/_ctypes/callproc.c
patching file Modules/_ctypes/ctypes.h
patching file Modules/_ctypes/callproc.c
patching file setup.py
patching file Mac/Modules/qt/setup.py
patching file setup.py
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.3.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/6f/08_5byqn69q2bpqx5x7vs1000000gn/T/python-build.20220418170004.43219
Results logged to /var/folders/6f/08_5byqn69q2bpqx5x7vs1000000gn/T/python-build.20220418170004.43219.log

Last 10 log lines:
        echo "generate-posix-vars failed" ; \
        rm -f ./pybuilddir.txt ; \
        exit 1 ; \
    fi
dyld[51418]: Library not loaded: /System/Library/Frameworks/Python.framework/Versions/2.7/Python
  Referenced from: /private/var/folders/6f/08_5byqn69q2bpqx5x7vs1000000gn/T/python-build.20220418170004.43219/Python-2.7.18/python.exe
  Reason: tried: '/System/Library/Frameworks/Python.framework/Versions/2.7/Python' (no such file), '/Library/Frameworks/Python.framework/Versions/2.7/Python' (no such file)
/bin/sh: line 1: 51418 Abort trap: 6           DYLD_LIBRARY_PATH=/var/folders/6f/08_5byqn69q2bpqx5x7vs1000000gn/T/python-build.20220418170004.43219/Python-2.7.18 ./python.exe -E -S -m sysconfig --generate-posix-vars
generate-posix-vars failed
make: *** [pybuilddir.txt] Error 1

It seems that is trying to reference the system's Python 2 which doesn't exist anymore.

Any ideas?
Thanks



Solution 1:[1]

This is how I got it fixed:

  1. Re-install command line tools:
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
  1. Run macOS updates to update the command line tools.

  2. Restart the computer. (Not sure if this had anything to do with it, but it didn't hurt).

  3. Install Python 2 with pyenv:

export PYTHON_CONFIGURE_OPTS="--enable-shared" && pyenv install 2.7.18

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dablak