PyEnv BUILD FAILED installing Python on MacOS

54,896

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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/

Solution 5

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

Share:
54,896
y2k-shubham
Author by

y2k-shubham

"A Big-Shot is a Little-Shot who keeps on Shooting, So keep Trying" - Dr. APJ Abdul Kalam, [Wings of Fire] My Bio Science Lover Introvert Technology Enthusiast Know me Web: y2k-shubham About.Me: y2k-shubham Connect with me GitHub: y2k-shubham Disqus: y2k_shubham . LinkedIn: y2k-shubham Twitter: y2k_shubham . Instagram: y2k.shubham Facebook: y2k.shubham

Updated on November 20, 2021

Comments

  • y2k-shubham
    y2k-shubham over 2 years

    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
  • brandonscript
    brandonscript over 5 years
    Might work, but that's super destructive, and probably cause you even more problems in the end...
  • Vadim
    Vadim over 5 years
    in my case it doesn't work, the error remains the same
  • R Claven
    R Claven over 5 years
    This works if you have the LATEST version of XCode AND if you have "agreed" to the new license!
  • wjwwood
    wjwwood about 5 years
    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...
  • psuzzi
    psuzzi about 5 years
    Also in my case did not work, but Srinivas Gowda solution did. Anyway, the link to the thread is useful.
  • osprey
    osprey almost 5 years
    Works. Hopefully it doesn't break other things.
  • Vladimir Jovanović
    Vladimir Jovanović almost 5 years
    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!
  • NeilG
    NeilG almost 5 years
    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
  • Matt Browne
    Matt Browne almost 5 years
    Works for Python 3.6.6 too. Thank you!
  • Kjell Hedström
    Kjell Hedström almost 5 years
    Worked great on Mojave 10.14.5. The other answers did not work.
  • Tachikoma
    Tachikoma over 4 years
    Worked great on macOS Mojave 10.14.6
  • WhooNo
    WhooNo about 4 years
    Unfortunately, not working for me on Mojave with Python 3.7.3 or 3.5.2
  • Noble Polygon
    Noble Polygon about 4 years
    This worked to help me get the AWS ebs cli installed if you get this on Mac OS catalina
  • Srikanth
    Srikanth almost 4 years
    Worked for me on macOS Catalina (10.15.3) with Python 3.7.3
  • Tony M
    Tony M over 3 years
    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...
  • Bendemann
    Bendemann about 3 years
    Anybody cares to explain what this does to newbies :)?
  • Marcel Wilson
    Marcel Wilson about 3 years
    Is there some way to setup the environment so we dont have to include SDKROOT and MACOSX_DEPLOYMENT_TARGET every time?
  • Brian Garr
    Brian Garr about 3 years
    I tried every one of these on my M1 chip Mini, and none of them worked.
  • Brian Garr
    Brian Garr about 3 years
    further, I don't have a Packages folder under CommandLineTools
  • Moshe
    Moshe almost 3 years
    The --patch is the only thing that worked for me. Thanks.
  • Nasir
    Nasir over 2 years
    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
  • HamiltonPharmD
    HamiltonPharmD over 2 years
    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
  • Julian H
    Julian H over 2 years
    This worked for me, thanks.
  • Eric Aya
    Eric Aya over 2 years
    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.
  • StatsNoob
    StatsNoob about 2 years
    This works on Big Sur 11.6.4 as well
  • ScottWelker
    ScottWelker about 2 years
    This worked for me, Monterey 12.3.1 with pyenv 3.9.6. I did not uninstall pyenv first.
  • Steve Vermeulen
    Steve Vermeulen almost 2 years
    In addition to these steps, I also had to run brew install openssl
  • Mote Zart
    Mote Zart almost 2 years
    I get this: By default, distutils will build C++ extension modules with "clang++". With the error checking for clang++... no. But clang/clang++ are 100% already installed globally. Running the code in this answer above prompts me to install clang, which seemed promising. Maybe my pyenv couldn't find clang++? But DOH! Still failed pyenv install
  • schroederadrian
    schroederadrian almost 2 years
    I was in the same situation of migrating and this was the only working solution.