1

iTerm2 can be scripted with Python

When I run Python REPL or Python script through iTerm2 Scripting menu on Apple Sillicon architecture (M2 in my case) it terminates with following error:

Bad CPU type

like in this example stacktrace:

2-3, 2:43:17.371 PM: /opt/homebrew/bin/zsh -c /Applications/iTerm.app/Contents/Resources/it2_api_wrapper.sh /Users/zxaos/.config/iterm2/AppSupport/iterm2env-3.7/versions/3.7.9/bin/python3 /Users/zxaos/.config/iterm2/AppSupport/Scripts/AutoLaunch/iTerm-DarkLight.py
12-3, 2:43:17.381 PM: + unset PYTHONPATH
12-3, 2:43:17.381 PM: + export PYTHONUNBUFFERED=1
12-3, 2:43:17.381 PM: + PYTHONUNBUFFERED=1
12-3, 2:43:17.381 PM: + /Users/zxaos/.config/iterm2/AppSupport/iterm2env-3.7/versions/3.7.9/bin/python3 /Users/zxaos/.config/iterm2/AppSupport/Scripts/AutoLaunch/iTerm-DarkLight.py
12-3, 2:43:17.381 PM: /Applications/iTerm.app/Contents/Resources/it2_api_wrapper.sh: line 6: /Users/zxaos/.config/iterm2/AppSupport/iterm2env-3.7/versions/3.7.9/bin/python3: Bad CPU type in executable
12-3, 2:43:17.382 PM: 
12-3, 2:43:17.382 PM: ** Script exited with status 126 **

How to solve it?

1 Answers1

0

Problem is that Python included with iTerm2 is on x86 CPU architecture, but you have ARM CPU (Apple Sillicon or aarm64 architecture). You probably don't have Rosetta2 (Intel x86 emulation) installed.

  1. Open 'Terminal'
  2. Type softwareupdate --install-rosetta --agree-to-license
  3. Run (Enter)
  • A much better answer would be to install a native copy of Python. – Marc Wilson Mar 21 '23 at 16:55
  • 1
    This is the second of your questions you have self-answered with the same answer. Starting to feel a bit spammy.... – Andy Griffiths Mar 21 '23 at 17:09
  • @AndyGriffiths Not spammy at all. This is how i find out about the problem. The second one is more generic because it's about how to install Rosetta through CLI. Both problems seems to me uncovered here on Stackexachange.com and even when you google it. This is why I made this twice. If you question me see my profile... – Michal Bernhard Mar 22 '23 at 15:21
  • @MarcWilson I don't know I am new on MacOs and not sure how iTerm2 cope with it's dependency on Python. Probably it's embedded in it, so your answer wouldn't work. – Michal Bernhard Mar 22 '23 at 15:22
  • @MichalBernhard Seriously, you missed this? https://support.apple.com/en-gb/HT211861 – Andy Griffiths Mar 22 '23 at 15:24
  • @MichalBernhard Running the REPL or a Python script within iTerm doesn't have amything to do with iTerm itself. – Marc Wilson Mar 23 '23 at 04:34
  • @MarcWilson https://iterm2.com/documentation-scripting.html ? – Michal Bernhard Mar 23 '23 at 15:54
  • @AndyGriffiths no I didn't. There is nothing about manual installation process (installation dialog shows only in case you run intel based app) hence I create this question+answer. – Michal Bernhard Mar 23 '23 at 15:56
  • 1
    @MarcWilson the issue is scripting iTerm itself shich the author of iTerm admist is a problem - not running python in iTerm whioch works with no problems. The issue is that iTerm as a good ~//mac app bundles python - unfortunately only the intel version – mmmmmm Mar 23 '23 at 16:18
  • 4
    The CLI installation process for Rosetta2 is widely documented online also. All that aside, a lot of this could have been avoided with a clearer question in the first place. Only now, with all the extra information you've added about the iTerm2 in-built Python and scripting against iTerm's own API does it make sense. – Andy Griffiths Mar 23 '23 at 19:51
  • @AndyGriffiths ok, thanks for your feedback – Michal Bernhard Mar 31 '23 at 07:47