Access pipeline variables in Azure Release for classic python task

Copper Contributor

I have a Python script that has url, token, location, and version as environment variables. I trigger it using the classic Python task. I declared these variables inside the pipeline variables section, and inside the code, I did the following:


import os


but I get keyerror: 'url'


I came across this link, which creates a variable group and passes the environment variables as arguments. But this step follows inline script based execution.



2 Replies


To access pipeline variables within a classic Python task in Azure DevOps, you can use the `system` prefix to access the variable. Here's how you can modify your code to retrieve the values of your pipeline variables:

import os

url = os.environ['SYSTEM_URL']
token = os.environ['SYSTEM_TOKEN']
location = os.environ['SYSTEM_LOCATION']
version = os.environ['SYSTEM_VERSION']

In Azure DevOps, pipeline variables are available as environment variables prefixed with `SYSTEM_`. By using the `SYSTEM_` prefix followed by the variable name, you can access the values of pipeline variables within your Python script.


Make sure that the pipeline variables are properly defined and accessible within the pipeline context. Double-check the variable names and ensure they are correctly defined in the pipeline variables section.


By accessing the pipeline variables with the `SYSTEM_` prefix in your Python script, you should be able to retrieve the values without encountering the `KeyError`.


If I have answered your question, please mark your post as Solved
If you like my response, please give it a like


Thanks for the answer but I still couldn't access the variables I get the same key error




this is pipeline variable is have defined, marked URL and token as secret just to share it here