`pio run` fails on MacOS with "No module named 'pkg_resources'"

I’ve been fighting this issue for a while on multiple Macs. When running pio run on a project, I get the error message below. I’ve tried this with a fresh install of Homebrew, followed by brew install platformio. I’ve also tried it with the get-platformio.py script. Same thing every time. I’ve deleted the .platformio in my home directory, removed the .pio directory in the project, cleaned all platformio references from my path, and anything else I could think of.

The three machines that fail are all running Sonoma. Oddly, I have a MacBook Pro from 2014 that does work, and is actually my only machine I can compile projects on.

Hoping someone has some insight. I’m guessing the error is something related to SConscript.py, but I don’t speak Python.

$ platformio run
**********************************************************************************************************************************************************************************************************************************************************
If you like PlatformIO, please:
- star it on GitHub > https://github.com/platformio/platformio-core
- follow us on LinkedIn to stay up-to-date on the latest project news > https://www.linkedin.com/company/platformio/
- try PlatformIO IDE for embedded development > https://platformio.org/platformio-ide
**********************************************************************************************************************************************************************************************************************************************************

Processing esp32-poe-iso (platform: espressif32; board: esp32-poe-iso; framework: espidf)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Platform Manager: Installing espressif32
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Platform Manager: espressif32@6.5.0 has been installed!
Tool Manager: Installing espressif/toolchain-xtensa-esp32 @ 12.2.0+20230208
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: toolchain-xtensa-esp32@12.2.0+20230208 has been installed!
Tool Manager: Installing platformio/toolchain-esp32ulp @ ~1.23500.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: toolchain-esp32ulp@1.23500.220830 has been installed!
Tool Manager: Installing espressif/tool-xtensa-esp-elf-gdb @ ~12.1.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-xtensa-esp-elf-gdb@12.1.0+20221002 has been installed!
Tool Manager: Installing espressif/tool-riscv32-esp-elf-gdb @ ~12.1.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-riscv32-esp-elf-gdb@12.1.0+20221002 has been installed!
Tool Manager: Installing platformio/framework-espidf @ ~3.50102.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: framework-espidf@3.50102.240122 has been installed!
Tool Manager: Installing platformio/tool-esptoolpy @ ~1.40501.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-esptoolpy@1.40501.0 has been installed!
Tool Manager: Installing platformio/tool-cmake @ ~3.16.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-cmake@3.16.4 has been installed!
Tool Manager: Installing platformio/tool-ninja @ ^1.7.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-ninja@1.9.0 has been installed!
Tool Manager: Installing platformio/tool-scons @ ~4.40600.0
Downloading  [####################################]  100%
Unpacking  [####################################]  100%
Tool Manager: tool-scons@4.40600.0 has been installed!
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32-poe-iso.html
PLATFORM: Espressif 32 (6.5.0) > OLIMEX ESP32-PoE-ISO
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-espidf @ 3.50102.240122 (5.1.2)
 - tool-cmake @ 3.16.4
 - tool-esptoolpy @ 1.40501.0 (4.5.1)
 - tool-ninja @ 1.9.0
 - tool-riscv32-esp-elf-gdb @ 12.1.0+20221002
 - tool-xtensa-esp-elf-gdb @ 12.1.0+20221002
 - toolchain-esp32ulp @ 1.23500.220830 (2.35.0)
 - toolchain-xtensa-esp32 @ 12.2.0+20230208
ModuleNotFoundError: No module named 'pkg_resources':
  File "/Users/jcw/.platformio/penv/lib/python3.12/site-packages/platformio/builder/main.py", line 173:
    env.SConscript("$BUILD_SCRIPT")
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Script/SConscript.py", line 609:
    return _SConscript(self.fs, *files, **subst_kw)
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Script/SConscript.py", line 279:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "/Users/jcw/.platformio/platforms/espressif32/builder/main.py", line 346:
    target_elf = env.BuildProgram()
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Util/envs.py", line 251:
    return self.method(*nargs, **kwargs)
  File "/Users/jcw/.platformio/penv/lib/python3.12/site-packages/platformio/builder/tools/piobuild.py", line 61:
    env.ProcessProgramDeps()
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Util/envs.py", line 251:
    return self.method(*nargs, **kwargs)
  File "/Users/jcw/.platformio/penv/lib/python3.12/site-packages/platformio/builder/tools/piobuild.py", line 121:
    env.BuildFrameworks(env.get("PIOFRAMEWORK"))
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Util/envs.py", line 251:
    return self.method(*nargs, **kwargs)
  File "/Users/jcw/.platformio/penv/lib/python3.12/site-packages/platformio/builder/tools/piobuild.py", line 347:
    SConscript(env.GetFrameworkScript(name), exports="env")
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Script/SConscript.py", line 673:
    return method(*args, **kw)
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Script/SConscript.py", line 609:
    return _SConscript(self.fs, *files, **subst_kw)
  File "/Users/jcw/.platformio/packages/tool-scons/scons-local-4.6.0/SCons/Script/SConscript.py", line 279:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "/Users/jcw/.platformio/platforms/espressif32/builder/frameworks/espidf.py", line 29:
    import pkg_resources
============================================================================================================== [FAILED] Took 69.82 seconds ==============================================================================================================

I believe that pkg_resources is part of setuptools.
Try using pip to install athat or, if it is installed, then uninstall it and install it again.
(This is based on multiple responses to a google search)
Susan

It should be fixed in the latest platform revision from dev branch (a Git client is required):

[env:esp32dev]
platform = https://github.com/platformio/platform-espressif32.git
framework = espidf
board = esp32dev
1 Like

That fixed it. I found a bug report I had submitted on PlatformIO’s Github on 2024-01-12 had been closed, although I don’t recall seeing a notification. The fix was made in the develop branch, but unless (as you say) platform= is setup to pull from Github, it won’t be applied. Apparently builder/frameworks/espidf.py included pkg_resources, but doesn’t use it.