Pre Build Script: Get Env Name

I am trying to create a pre-build script in a python file.

I import the env using Import(“env”) as specified in the documentation, however I would like to assign a variable in python to the name of the currently selected environment.

Example:

platformio.ini:

[env:pico]
platform = …

python:

Import(“env”)
print(env.)

I would like the script to be printing “pico”

How do I go about accessing this name from my pre-build script?

The name of the environment is stored in the $PIOENV variable inside env (see code), so you can just do

Import("env")
print(env["PIOENV"])

or equivalentely

Import("env")
print(env.subst("$PIOENV"))

You could have also found that out by going into the PlatformIO project, opening a CLI and asking for a dump of the environment variables

> pio run -t envdump
[...]
  'PIOBUILDFILES': [[<SCons.Node.FS.Entry object at 0x000001EA4C282850>]],
  'PIOENV': 'uno',
  'PIOFRAMEWORK': ['arduino'],
  'PIOMAINPROG': [<SCons.Node.FS.File object at 0x000001EA4C27F410>],
  'PIOPLATFORM': 'atmelavr',
[...]

And it would have been found in the documentation too

https://docs.platformio.org/en/latest/projectconf/sections/env/options/build/build_flags.html#built-in-variables

Excellent thanks for the support @maxgerhardt

I did actually do a dump printout after I sent the message but failed to spot the entry (I am not very observant).

Many thanks!