pythonvariablesbitbucketbitbucket-pipelines

Accessing BitBucket Variables directly inside a Python Script


I've set up some Repository Variables (also tried deployment variables) that hold some secrets for DB connections.

I would like to use these variables directly in my .py file, trouble is I have no idea how to, I tried accessing them a few different ways and the error is always the variables being null. Although if I expr the variables in the .yml file it is being read correctly.

Current Config:

enter image description here

inside of main.py

connection_parameters = {
    "account": os.getenv('$account'),
    "user": os.getenv('$user'),
    "password": os.getenv('$password')

Proof the variable can be accessed from .yml file(it has the value of 10):

enter image description here

So I guess my question is, how do I access these variables stored on BitBucket, inside of my python script, upon deployment?


Solution

  • When using os.getenv you should use the variable name only. The $ is a part of unix shell syntax and not necessary for accessing variables in Python.

    connection_parameters = {
        "account": os.getenv('account'),
        "user": os.getenv('user'),
        "password": os.getenv('password')
    }