Couldn't posix_spawn: error 2
Asked by Tom Bouquet:

Keep getting this error when trying to clone my Github repos. Any help would be appreciated! I am running on MacOS (M1)

```
Couldn't posix_spawn: error 2
```


Answers:
author: Frankie
body: Since macOS 12.3 removed the built-in Python@2.x, Sourcetree cannot open Mercurial projects normally after system upgrade.

Guess the reason is that the built-in Mercurial of Sourcetree depends on the built-in Python2 of macOS, but Python2 was removed, so it throws an error.

**The solution is as follows:**

1. Open Sourcetree preferences and switch to the Mercurial tab.

You can see the error message from Mercurial Version: **Embedded Mercurial Couldn't posix_spawn: error 2**.

2. Select **Use System Mercurial** ?path like: **/usr/local/Cellar/mercurial/6.1/lib/python3.10/site-packages/mercurial**.

Choose according to the version of Mercurial you have installed. If you installed Mercurial using Homebrew, you can find its path through **brew list mercurial**.

That's it.

![iShot2022-03-22 23.09.11.png](https://community.atlassian.com/t5/image/serverpage/image-id/189429i7E7894BD2AFE5380/image-size/large?v=v2&px=999 "iShot2022-03-22 23.09.11.png")

comments: author: j c
body: This doesn't seem to work. I get the same error using the Brew-installed version of Mercurial.

On my system it was installed at:

/opt/homebrew/Cellar/mercurial/6.1/lib/python3.10/site-packages/mercurial/

...but once I select that folder in SourceTree, it says the same error: "System Mercurial Couldn't posix_spawn: error 2"

Maybe it's still trying to use Python 2.7 still instead of Python 3.10?


author: mapz
body: Worked for me!


author: veraZhang
body: thanks!


author: F?bs F?berson
body: If I choose the mercurial location from brew I get the 'not found' error and am not able to select it in Sourcetree.


author: j c
body: The only way I was able to get it to work was to modify the "hg_local" file inside the SourceTree.app package and replace the first line to point to the path of Python 3.10 that I installed via Homebrew \<<https://brew.sh>\>. And then set it to use the Embedded Mercurial.

The steps:

1. Install Python 3.10 via Homebrew \<<https://brew.sh>\>, or skip this and just try with the Python you already installed.

2. Select your copy of SourceTree.app in the Finder and right-click (aka Control-click) on the icon and choose "Show Package Contents".

3. Navigate to Contents/Resources/mercurial_local/, then open "hg_local" in a text editor (BBEdit or TextEdit). You'll have to drag and drop it onto the editor icon, as the Mac will only want to open the file in Terminal by default.

4. Change the first line to the path of the Python you installed. (I found Python 3.10 works fine with the embedded Mercurial.)

For example, change line 1 from:

#!/usr/bin/python

to:

#!/opt/homebrew/opt/python@3.10/bin/python3.10

(Note, this is where Python 3.10 installed for me, but might be very different for you. Confirm first.)

...and save the file.

5. Launch SourceTree and make sure you are in Embedded Mercurial mode, in Preferences \> Mercurial \> "Embedded Mercurial Version 4.6.1".

6. Don't let SourceTree update until they have fixed this issue themselves, otherwise it will undo the above edit on step #4 (or redo the edit after each update.)


author: Stephen Douglas Scotti
body: Same issue for me, although I just had to change hashbang /she-bang to:  

#!/usr/bin/python3, restart SourceTree and wait for it to be 'fixed' in the distro package. I installed hg with homebrew also. M1 Mac Monterey.


author: sunil_pandith
body: IT WORKED FOR ME!!! I installed mercurial first using "brew install mercurial" then uninstalled the SourceTree and installed it back again!! Hope it helps !!!


author: j c
body: Be aware that they officially released a new version 4.2 that works without modification, and at full speed for M1 Macs.


author: Frank Vyncke
body: Same problem after upgrading to 12.3... I am still on Intel. Git works, Mercurial does not

comments: author: Frank Vyncke
body: it's actually related to the fact that Apple seems to have upgraded the default python to version 3 (at last).

But that breaks any hg (also on the command line).

On command line I can fix it by installing mercurial 6.1, but I cannot select that version as the external mercurial... Sourcetree reports 'mercurial not found in selected folder?)


author: Frankie
body: Me too, after upgrading macOS 12.3, opening the Mercurial project in sourcetree will report an error. But the Git project works fine.


author: Jiajun
body: Same here. Looks like a recent MacOS update causes the issue.

comments: author: Yuri
body: Yes, I have M1 and macOS 12.3


author: Jiajun
body: Has anyone reported the issue to Atlassian? Otherwise it will take forever to get it fixed.


author: Yuri
body: Your turn. )


author: DC
body: Same problem, I still can't fix this problem after adding the existing local repo on macOS 12.4 Apple M1 and SourceTree 4.1.8 (244).

And I get the 'not found' error when I choose the mercurial location from brew. I guess SourceTree don't support Mercurial 6.1.3 by Python 3.8.9.

Looks like official noticed this problem: <https://jira.atlassian.com/browse/SRCTREE-7867>. Let's look forward to hearing from their good news.


author: Yuri
body: Same problem.

comments: author: Tom Bouquet
body: Mine magically fixed itself after adding an existing local repo. If it wasn't that then idk what fixed it.


author: veraZhang
body: same problem. what is the solution? thanks


Tags: atl-gruland