1

I am using below code to get environment variable in Azure Devops pippelines, using ubuntu latest vmimage.

os.environ.get("xyz")

But it's returning None value. When I print os.environ then I can see the xyz key and value, I am not sure what's happening wrong here.

"xyz" is added in the Azure pipeline by using Azure Variable Group.

Python (Linux) 3.8.10 | packaged by conda-forge | (default, May 11 2021, 07:01:05)

Please guide. Thanks

Deepak
  • 303
  • 3
  • 14
  • where do you print `os.environ` from? the same script? – gold_cy Jul 22 '21 at 18:03
  • Yes, in same execution context, same script. os.environ gives values but os.environ.get("xyz") not. – Deepak Jul 22 '21 at 18:03
  • 1
    Please provide a complete example. We have only your word that the exact name `xyz` is in the environment. How was the name added to the environment in the first place? – chepner Jul 22 '21 at 18:07
  • "xyz" is added in the Azure pipeline by using Azure Variable Group. – Deepak Jul 22 '21 at 18:08
  • Does `os.getenv('xyz')` give the same result? – 0x5453 Jul 22 '21 at 18:12
  • Also worth noting [this](https://docs.python.org/3/library/os.html#os.environ) behavior of `os.environ`: *"This mapping is captured the first time the os module is imported, typically during Python startup as part of processing `site.py`. Changes to the environment made after this time are not reflected in `os.environ`, except for changes made by modifying `os.environ` directly."* – 0x5453 Jul 22 '21 at 18:14
  • Yes, os.getenv('xyz') also gives None – Deepak Jul 23 '21 at 01:52
  • Both os.getenv('xyz') and os.environ are running one after another in the script, so there should not be any impact of site load mapping behavior. – Deepak Jul 23 '21 at 01:55

0 Answers0