MacOS Platformio Build Failing Due to Python2

I’m attempting to build Marlin using PlatformIO. When I run a build in VSCode or in terminal I get:

wcunning@mycroft-holmes Marlin-2.0.7.2-SKR-mini-E3-V1.2 % pio run
Processing STM32F103RC_btt_512K (platform: ststm32@~6.1; board: genericSTM32F103RC; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x!
     PYTHONPATH is currently: "/usr/local/lib/python3.9/site-packages"
     You should `unset PYTHONPATH` to fix this.
OSError: 'python Marlin/src/HAL/STM32F1/build_flags.py' exited 1:
  File "/usr/local/lib/python3.9/site-packages/platformio/builder/main.py", line 175:
    env.SConscript(item, exports="env")
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Script/SConscript.py", line 591:
    return _SConscript(self.fs, *files, **subst_kw)
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Script/SConscript.py", line 280:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "/Users/wcunning/Documents/3d_print/skr_firmware/BIGTREETECH-SKR-mini-E3/firmware/V1.2/Marlin-2.0.7.2-SKR-mini-E3-V1.2/buildroot/share/PlatformIO/scripts/common-dependencies.py", line 286:
    apply_features_config()
  File "/Users/wcunning/Documents/3d_print/skr_firmware/BIGTREETECH-SKR-mini-E3/firmware/V1.2/Marlin-2.0.7.2-SKR-mini-E3-V1.2/buildroot/share/PlatformIO/scripts/common-dependencies.py", line 118:
    if not env.MarlinFeatureIsEnabled(feature):
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Util.py", line 658:
    return self.method(*nargs, **kwargs)
  File "/Users/wcunning/Documents/3d_print/skr_firmware/BIGTREETECH-SKR-mini-E3/firmware/V1.2/Marlin-2.0.7.2-SKR-mini-E3-V1.2/buildroot/share/PlatformIO/scripts/common-dependencies.py", line 262:
    load_marlin_features()
  File "/Users/wcunning/Documents/3d_print/skr_firmware/BIGTREETECH-SKR-mini-E3/firmware/V1.2/Marlin-2.0.7.2-SKR-mini-E3-V1.2/buildroot/share/PlatformIO/scripts/common-dependencies.py", line 233:
    build_flags = env.ParseFlagsExtended(build_flags)
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Util.py", line 658:
    return self.method(*nargs, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/platformio/builder/tools/platformio.py", line 185:
    for key, value in env.ParseFlags(str(raw)).items():
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Environment.py", line 821:
    do_parse(arg)
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Environment.py", line 673:
    arg = self.backtick(arg[1:])
  File "/Users/wcunning/.platformio/packages/tool-scons/scons-local-4.1.0/SCons/Environment.py", line 585:
    raise OSError("'%s' exited %d" % (command, status))
==================================================================== [FAILED] Took 0.46 seconds ====================================================================

Environment           Status    Duration
--------------------  --------  ------------
STM32F103RC_btt_512K  FAILED    00:00:00.461
=============================================================== 1 failed, 0 succeeded in 00:00:00.461 ===============================================================

As a sanity check, I ran the following:

wcunning@mycroft-holmes Marlin-2.0.7.2-SKR-mini-E3-V1.2 % which python
python: aliased to /usr/local/bin/python3
wcunning@mycroft-holmes Marlin-2.0.7.2-SKR-mini-E3-V1.2 % python --version
Python 3.9.1
wcunning@mycroft-holmes Marlin-2.0.7.2-SKR-mini-E3-V1.2 % pio system info
--------------------------  ---------------------------------------
PlatformIO Core             5.1.0
Python                      3.9.1-final.0
System Type                 darwin_x86_64
Platform                    macOS-11.2
File System Encoding        utf-8
Locale Encoding             UTF-8
PlatformIO Core Directory   /Users/wcunning/.platformio
PlatformIO Core Executable  /usr/local/bin/platformio
Python Executable           /usr/local/opt/python@3.9/bin/python3.9
Global Libraries            0
Development Platforms       2
Tools & Toolchains          12

Why the heck is it still thinking that it’s running Python2.7? This is OSX 11.2 with Homebrew recently updated.

Hm seems like a general setup issue with Python here and not specifically PlatformIO. The error message is thrown by Python directly, not PlatformIO.

See e.g. here or here.

Not sure how to fix it though. Maybe @ivankravets has some ideas.

What does it output when execute this command manually in the CLI (and pwd is in the project folder)?

So it turns out that I somehow had multiple copies of PIO installed from different sources. After I uninstalled the homebrew one and blew away the ~/.pio directory and reinstalled from homebrew, I had a functional setup. I think there was some out-of-date config file that was pointing to the wrong version of python.

To respond to the specific suggestion, running that Marlin/src/HAL/STM32F1/build_flags.py script in the terminal worked fine, and running which python and the like pointed to the right version to run fine, which was why I was stumped. Fortunately hard reset seems to have fixed everything.