87

While trying to install Python 3.6.6 (for Airflow) using PyEnv on MacOS, I am encountering build failure with following stack-trace

File
"/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xmlrpc/client.py",
line 138, in <module>
    from xml.parsers import expat   File "/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xml/parsers/expat.py",
line 4, in <module>
    from pyexpat import * ModuleNotFoundError: No module named 'pyexpat' make: *** [install] Error 1

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

Inspect or clean up the working tree at
/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347
Results logged to /var/folders/6y/kf699bqj2sgcgjsh

I've followed the steps mentioned in the docs for setting-up PyEnv on Mac via Homebrew.


Environment / Framework versions

  • MacOS High Sierra 10.13.6
  • Homebrew 1.7.1
  • PyEnv 1.2.6
  • Existing Python distributions
    • Python 2.7.15 [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
    • Python 3.7.0 [Clang 9.0.0 (clang-900.0.39.2)] on darwin
2
  • Same issue, just had to open a new terminal window. Had some bad environment variables from when I ran pyenv uninstall in the same session. Commented Jul 17, 2022 at 19:04
  • Looking into this further and providing more clarification, I'm running this env: ``` Homebrew 4.4.19 arch: arm64 pyenv 2.5.1 macOS 15.2 ``` I've gotten into the headers for gettext which seem to be the culpit and the C externs exist in /usr/local/include/libintl.h ``` /* Set the current default message catalog to DOMAINNAME. If DOMAINNAME is null, return the current default. If DOMAINNAME is "", reset to the default of "messages". */ #ifdef _INTL_REDIRECT_INLINE extern char *libintl_textdomain (const char *__domainname); # ifndef __cplusplus static # endif ``` Commented Feb 3 at 5:04

24 Answers 24

45

I had this problem with Mojave and Python 3.7.3.

This worked for me:

SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk MACOSX_DEPLOYMENT_TARGET=10.14 pyenv install 3.7.3
Sign up to request clarification or add additional context in comments.

10 Comments

Worked great on Mojave 10.14.5. The other answers did not work.
Worked great on macOS Mojave 10.14.6
Worked for me on macOS Catalina (10.15.3) with Python 3.7.3
Anybody cares to explain what this does to newbies :)?
Is there some way to setup the environment so we dont have to include SDKROOT and MACOSX_DEPLOYMENT_TARGET every time?
|
40

I'm posting my solution for those who are trying to resolve this issue in 2021. I am using macOS Big Sur Version 11.5.1. For me, this worked:

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

    $ pyenv install 3.9.5

enter image description here

6 Comments

I can confirm this worked for me in MacOS Big Sur Ver 11.5.2. I uninstalled pyenv prior to performing the above steps to be safe then reinstalled after step 2 above with: curl https://pyenv.run | bash
This works on Big Sur 11.6.4 as well
This worked for me, Monterey 12.3.1 with pyenv 3.9.6. I did not uninstall pyenv first.
In addition to these steps, I also had to run brew install openssl
Worked for me on Monterey 12.5.1, for python 3.10.6
|
35

The only way I could get it working is through the below prefixes before installing

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include 
-I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" \
PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 \
pyenv install -v 3.7.1

Here's a breakdown of what each part of the command does:

  1. CFLAGS: This is an environment variable that specifies additional flags to be used by the C compiler when compiling Python.

-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include: These flags tell the compiler where to find the header files for readline, openssl, and the SDK (Software Development Kit) provided by Xcode.

The -I option adds directories to the list of directories to be searched for header files.

  1. LDFLAGS: This is another environment variable that specifies additional flags for the linker.

  2. -L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib: These flags tell the linker where to find the libraries for readline and openssl. The -L option adds directories to the list of directories to be searched for libraries.

  3. PYTHON_CONFIGURE_OPTS: This environment variable is used to pass additional options to the configure script when building Python.

--enable-unicode=ucs2: This option configures Python to use UCS-2 (2-byte Unicode) as the internal representation for Unicode characters. This can be important for compatibility with certain Python packages or systems.

pyenv install -v 3.7.1: This is the actual command to install Python 3.7.1 using pyenv.

pyenv is a tool that allows you to easily install and manage multiple versions of Python.

-v is an option for verbose logging, which provides more detailed output during the installation process.

3 Comments

If you're copy-pasting the snippet above, make sure to remove the new line at the end of the first line which can break the compilation
what does this actually do?
@mattsmith5 Updated the answer with breakdown of each command
15

I was able to get away with above error by referring to Common build problems

  • brew install readline xz
  • xcode-select --install
  • brew install openssl

After this, I installed and initialized pyenv

  • brew install pyenv
  • pyenv init (inside your project directory)

Finally install and activate required python version

  • pyenv install 3.6.6
  • pyenv local 3.6.6 (inside your project directory)

Here's the link to original thread #1188 on GitHub by @Harry Moreno

5 Comments

in my case it doesn't work, the error remains the same
This works if you have the LATEST version of XCode AND if you have "agreed" to the new license!
Also in my case did not work, but Srinivas Gowda solution did. Anyway, the link to the thread is useful.
This worked to help me get the AWS ebs cli installed if you get this on Mac OS catalina
What am I even doing anymore.. I need to install readline so that I can install pyenv so that i can install a python version so I can install a node version so that i can run a test.
12

The accepted answer didn't work for me (Mojave) but this did:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Credit to http://www.blog.howechen.com/macos-mojave-pyenv-install-multi-version-build-failed-solution/

3 Comments

I suppose that might work, but is that a good idea? Doesn't this prevent you from using different SDK versions at different times? Presumably Apple changed this for a reason...
Works. Hopefully it doesn't break other things.
This is a known requirement for Mojave as described here github.com/pyenv/pyenv/wiki/Common-build-problems which I routinely completed during initial installation but still got the same error
10

UPDATE FOR 2023

Python 3.6.X don't seem to support Apple Silicon. According to release note, python support Apple Silicon from 3.9.1.

SOLVED with the help of arch command:

arch -x86_64 pyenv install 3.8.12

Related to this issue: https://github.com/pyenv/pyenv/issues/1876

Comments

9

install Xcode:

xcode-select --install

install Brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

OR

update brew:

brew upgrade

install packages:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force
brew install openssl

Install Pyenv

curl https://pyenv.run | bash

Insert the following lines to .bashrc/.zshrc config file:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

restart the terminal and execute:

pyenv install 3.6.5

If still doesn't work: (just change the python version to the desired one - in my case i've installed python 3.6.5 to pyenv)

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.5 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

2 Comments

The --patch is the only thing that worked for me. Thanks.
Thanks, this part did the trick for me: brew install libxml2 brew install libxslt brew link libxml2 --force brew link libxslt --force
3

short answer:

$ brew upgrade

now you can try to install python through pyenv

$ pyenv install 3.7.4

then, you have to set the python path

pyenv global 3.7.4

now, close and open a new terminal and write the command

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

That's all. For me it was fine.

1 Comment

I tried this and it didn't work. The brew upgrade part worked fine (took a while) but then pyenv install 3.7.4 gave BUILD FAILED (OS X 11.1 using python-build 20180424) Inspect or clean up the working tree at /var/folders/m8...
2

In my case this was caused by migration of data from old to new Mac. Both Xcode Command Line Tools and Homebrew migrated, but something broke. Reinstalling them from scratch worked for me.

Reinstall CLI tools:

$ sudo rm -rf /Library/Developer/CommandLineTools

$ xcode-select --install

Reinstall and update Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

Istall pyenv + packages and python 3:

brew install pyenv
brew install openssl readline sqlite3 xz zlib
pyenv install 3.7.5

1 Comment

I was in the same situation of migrating and this was the only working solution.
2

The cause is a change in Apple Clang 13.0.0+ that made it incompatible with existing Python versions. Upgrade to the aforementioned newest Python versions to fix the issue.

Please try the following suggestion https://github.com/pyenv/pyenv/issues/2112#issuecomment-1096478446

Comments

1

same issue solved following a comment on pyenv issue tracking system suggesting to apply this patch: https://github.com/python/cpython/commit/8ea6353.patch

# original comment
pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

it did work for 3.6.9 and 3.7.4 versions as well e.g.

pyenv install --patch 3.6.9 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

source: https://github.com/pyenv/pyenv/issues/1738#issuecomment-880517959

Comments

1

I tried xcode install, brew upgrade etc, nothing worked. In the end , this patch worked (for MacOS Monterey)

pyenv install --patch 3.8.5 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)

1 Comment

Welcome to stackoverflow! In oder to make your answer useful for others please format code as code with backtick-markups. Btw the link in your script seems to be broken.
1

I just had outdated prerequisites on MacOS 12.6

brew install openssl readline sqlite3 xz zlib tcl-tk

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

Comments

1

Following these 3 steps also solved my problem of installing pyenv 3.10.6 for macOS Monterey 12.6.5

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

$ pyenv install 3.10.6

Comments

1

I had to uninstall and reinstall Home Brew before it returned to work. It concerned the change from Mac Intel to Mac M1 (Silicon). See the article below from Josh Alletto to find out why.

HomeBrew on Mac M1/M2

Comments

0

After upgrading to Big Sur you need to reinstall X Code tools. Even if you had done it previously

xcode-select --install

This worked for me

If that doesn't work, force it to reset

sudo xcode-select --reset

More details can be found here: https://dev.to/o9uzdev/macos-xcrun-error-invalid-active-developer-path-missing-xcrun-411a

Comments

0

Try any of these:

❯ xcode-select --install
❯ xcode-select --reset
❯ export SDKROOT="macosx"

Maybe you don't have command line tools installed.
Or maybe you messed up the $DEVELOPER_DIR environment
variable path.
Or maybe your compiling with command line tools meant
for iPhone development or WatchOS !?!
❯ brew update && brew upgrade pyenv

Maybe your command versions aren't up to date ?
Double check you've properly followed the installation
steps for your version, OS and shell.
❯ brew install make cmake;
❯ export PATH="/usr/local/opt/make/libexec/gnubin:$PATH"

Maybe you don't have `make` and `cmake` build/compilation commands.
Or maybe they can't be found.
❯ brew install openssl readline tcl-tk sqlite3 xz zlib bzip2;
❯ export PATH="/usr/local/opt/bzip2/bin:$PATH";
export CPPFLAGS="\
    -I$(xcrun --show-sdk-path)/usr/include \
    -I$(brew --prefix openssl)/include \
    -I$(brew --prefix readline)/include \
    -I$(brew --prefix zlib)/include \
    -I$(brew --prefix bzip2)/include";
export LDFLAGS="\
    -L$(xcrun --show-sdk-path)/usr/lib \
    -L$(brew --prefix openssl)/lib \
    -L$(brew --prefix readline)/lib \
    -L$(brew --prefix zlib)/lib \
    -L$(brew --prefix bzip2)/lib";

Maybe you're missing some suggested `pyenv` build,
tools or what not. Or maybe they can't be found during
compilation.
https://github.com/pyenv/pyenv/wiki#suggested-build-environment
❯ unset LIBRARY

Maybe you've set/defined a LIBRARY environment variable
(either in your shell initialization/startup files,
e.g. .bashrc, .zshrc, etc; or shell session).
This can be bad because it could be used in the build/compilation
process and may crash it.
❯ pyenv install --verbose --patch 3.8.11 \
    < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Maybe you haven't heard of the magical patch yet ?
which fixes issue with build process not taking into
account certain MacOS versions.
Also using the `--verbose` option, might give you
better hints as to what's going wrong.

❯ pyenv global 3.8.11 && eval "$(pyenv init -)"
OR  
❯ pyenv global 3.8.11 && exec $SHELL --login

Also, don't forget to set your preferred global
python interpreter version and reinitialize `pyenv`
or restart your [login] shell altogether.

It may still not work, in that case just keep looking and post your answer when you've found it. Best Wishes.

Here are some of the resources I've consulted:

Comments

0
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix 
bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk- 
path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew -- 
prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix 
bzip2)/lib" pyenv install --patch 3.6.12 < <(curl -sSL 
https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

change the version you want

Comments

0

I had the same error and this command worked for me, note that you need to change python version to your desired one to install, after --patch

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" \
pyenv install --patch 3.8.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Comments

0

On Ventura 13.3.1 with XCode 14.1 I was able to get it resolved by:

  1. Open XCode -> Settings -> Locations
  2. Select the latest Command Line Tools
  3. Open new terminal and pyenv install 3.10.6

Comments

0

This drove me absolutely crazy. For me the answer was that I accidentally had an older version of clang/llvm installed with homebrew.

brew unlink llvm

Comments

-1

for the recent googler, if you are using Big Sur, you should run the command

xcode-select --install

1 Comment

This has already been mentioned in many other answers. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.
-1

This steps helped me solving BUILD FAILED (OS X 13.0.1 using python-build 20180424)

  • First step run xcode-select --install
  • Second step brew install openssl@3
  • Finally brew install llvm and follow instructions for setting appropriate environment variables (in the output of the brew install command).

Comments

-3

This worked for me:

  1. remove CommandLineTools folder under /Library/Developer
  2. run in terminal: xcode-select --install
  3. sudo mv /usr/local/include /usr/local/include_old

3 Comments

Might work, but that's super destructive, and probably cause you even more problems in the end...
This is the only option that worked for me, but be cautious - after you perform this step, you need to recreate include dir and then reinstall everything from start with brew. Thanks @Tomer, this saved me from complete OS reinstall!
further, I don't have a Packages folder under CommandLineTools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.