Custom script needs access to $PIOENV variable

Hi All,

I have a script that works great as a pre-build process which uses the $PIOENV variable to dynamically select the correct target and such for processing some files before compilation. This works fine as the script is called by the PIO engine which defines the $PIOENV variable before running the build script.

I now have a requirement that I need a python script to be ran (like a tool) by the user but without going through the PIO build process.

My question is:

Is there any way that a python script ran in isolation can “hook” into the running PIO instance and get a reference to the $PIOENV variable?

Kind Regards,


I think the “currenetly selected environment” is held in the PlatformIO VSCode extension’s state, i.e., While I am not certain of it, I don’t think there’s a convenient exposed API that lets you query that information.

You can at any time though run pio run -t envdump to get a list of all available PIOENV values, after which you can have the user select the one they want.