env: python: No such file or directory when building app with Xcode
Asked Answered
M

8

49

When I build/run/archive my app in Xcode (on MacOS 12.3) I encounter this error:

env: python: No such file or directory
Command Ld failed with a nonzero exit code

I think I might have changed something with regard to my python environment while working on a school project or messed something up there. However, I can not figure out what is wrong.

I tried reinstalling Xcode and python (using brew and pyenv). I also relinked python using brew. But I still encounter the same error.

Which python gives the following results:

which python3
-> /usr/local/bin/python3

And in my ~/.zshrc I have the following line:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

Any help would be appreciated! If I missed or forgot anything please let me know, I'm quite new to this.

Mansoor answered 14/3, 2022 at 13:30 Comment(2)
Are you sure that what's in .zshrc affects Xcode? I don't know, but I would not bet on it, as Xcode is not normally launched from a shell.Eduction
Yes I don't think that should matter. However I added it as I'm quite new and normally I run the NativeScript project in the terminal (which uses Xcode), so I thought that might be affected by it. But indeed, also running it in Xcode also triggers this error.Mansoor
B
91

Homebrew only installs the binary python3, just to be safe. Xcode is complaining about a lack of the binary python (note the lack of a 3!).

You have a couple of options:

  1. When installing python3, Homebrew also creates a libexec folder with unversioned symlinks, such as python (what you're missing). Note the Caveats printed when installing it:

    $ brew info python
    [email protected]: stable 3.9.10 (bottled)
    ==> Caveats
    Python has been installed as
      /opt/homebrew/bin/python3
    
    Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
    `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
      /opt/homebrew/opt/[email protected]/libexec/bin
    
    See: https://docs.brew.sh/Homebrew-and-Python
    

    You could add this directory to your $PATH, such that python and pip become available; something like the following might suffice:

    echo 'export PATH="'"$(brew --prefix)"'/opt/[email protected]/libexec/bin:$PATH"' \
       >>~/.bash_profile
    

    ... although that will need to be modified according to your precise version of Python3, your shell of choice, etc.

  2. Alternatively and more simply, although a little more jankily, you could simply manually create the appropriate symlinks:

    ln -s "$(brew --prefix)/bin/python"{3,}
    
Ballarat answered 15/3, 2022 at 19:25 Comment(6)
Can't work on MacBook Air m1. Please refer to github.com/bazelbuild/bazel/issues/15510Sakhuja
Awesome, #1 didn't work for me even though I have 3.9, but #2 did.Ptisan
@劉大為 I'm not sure what you mean — the above works precisely as it's supposed to on an M1; that's what I'm typing on, right now. That's why the snippets use brew --prefix, in fact. That said, modifying $PATH is difficult to explain predictably; there's lots and lots of variance on different systems as to how the final value comes into existence — if it didn't work, I suggest you dive into debugging your $PATH settings.Ballarat
I've been stuck for few hours until I ran the above command. Worked great on M1 macbook pro.Carbide
second alternative worksMercymerdith
and if you are using mac ports sudo port select --set python python311 or sudo port select --set python python310Selfless
F
35

Add -f to be effective.

ln -s -f /usr/local/bin/python3 /usr/local/bin/python
Flatter answered 6/6, 2022 at 21:47 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Elly
this worked, be sure to add 'sudo' before this command if you're in macOS montereyHypophyge
worked for me without sudo!Troupe
R
21

I had posted the same question on nativescript official github and the solution that worked for me was in the answer by the user shilik

Monterey 12.3 removes python 2. All you need to do is to reinstall python2 back to system from this link https://www.python.org/downloads/release/python-2718/

Refuse answered 19/3, 2022 at 10:16 Comment(3)
@aaossa I have removed the "question part" of my previous post: was intended to give only a little bit of (my) context to the original question because I think that was the same problem. Now I left only the answer, did you mean this in your comment?Refuse
Upgrading to Monterey messed me up too. This is what I did... 1) Install pyenv with brew install pyenv 2) Install Python 2.7 with pyenv install 2.7.18 and then pyenv global 2.7.18 3) Then set the default paths typing in the console export PATH="${HOME}/.pyenv/shims:${PATH}"Damico
@Damico You are the man / or gal ! Thanks :)Wendell
T
21
  1. install python3
  2. run 'ln -s /usr/bin/python3 /usr/local/bin/python',Create a link to Python
Temperate answered 14/4, 2022 at 3:56 Comment(4)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Elly
Worked for me on Monterey! ln -s /usr/bin/python3 /usr/local/bin/pythonProceeding
@bibo bode - this gives me 'Permission denied'Hypophyge
You may have to use sudo before the command @HypophygeProceeding
B
11

For me the problem was with missing python env: python: No such file or directory

BUT in the end missing was python version 2.x after updating to macOS Monterey 12.5 (21G72). Problem was resolved by installing python from: https://www.python.org/downloads/release/python-2718/

What I've also tried but you probably don't have to do:

  • sudo brew install python
  • sudo brew upgrade
  • sudo ln -s -f /usr/local/bin/python3 /usr/local/bin/python
  • sudo ln -s $(which python3) /usr/local/bin/python
  • sudo ln -s $(which python3) /Applications/Xcode.app/Contents/Developer/usr/bin/python
  • sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/python3 /Applications/Xcode.app/Contents/Developer/usr/bin/python
Baker answered 5/8, 2022 at 21:28 Comment(1)
Thanks! "sudo ln -s $(which python3) /usr/local/bin/python" helped meTwentieth
S
4

This took me days of head scratching, and none of the solutions I found on the internet worked.

Eventually what DID work for me was this:

sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/python3 /Applications/Xcode.app/Contents/Developer/usr/bin/python

I used the find command to find all instances of python in the file hierarchy:

find / -name python*

and I saw that there was a symbolic link labelled python3 in /Applications/Xcode.app/Contents/Developer/usr/bin/ that was linked to a python instance deep within the bowels of Xcode.

However there was no symbolic link labelled python which seems to be what Xcode is looking for.

So I created a symbolic link linking python to python3 and that did the trick.

For what it's worth, I installed python via pyenv which I installed through homebrew on a 2020 Mac mini M1.

Sarnoff answered 30/6, 2022 at 11:6 Comment(0)
C
3

In my case, created symbolic link for dev_appserver.py like below.

ln -s /opt/local/bin/python2.7 /usr/local/bin/python
ln -s /opt/local/bin/python2.7 /usr/local/bin/python2

Command location and version should be adapted to your environment.

Cassiecassil answered 15/8, 2022 at 23:43 Comment(0)
S
3

I was able to solve this issue with the above-mentioned answers.

In my case, while I was trying npm install in my node project and was facing this issue.

Note: % brew install python is a prerequisite for all the below steps! Test if python is correctly installed by brew info python

  1. First thing which comes to mind is if python is correctly installed and the path is set correctly.

python --version was giving zsh - python not found error while python3 --version was a success.

  1. Next steps were to set the correct path. I did the below steps and it worked:
  • echo "alias python=/usr/bin/python3" >> ~/.zshrc
  • ln -s -f "$(brew --prefix)/bin/python"{3,}
  • ln -s -f "$(which python3)"{3,}

What I was missing was to run brew install python, and it worked like a charm!

Siderite answered 20/10, 2022 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.