Do I need to define a repo secret in a YAML file when accessing via a Python script?
Asked by Sharon Machlis:

I'm having some trouble accessing my secrets in a Python script and don't know if I need to do anything in my Pipeline YAML file? I've seen conflicting advice on this.

Specifically: If I save a BitBucket repository secret value called MY_SECRET by going to Repository settings \> \[under the Pipelines section\] Repository Variables, do I need to add anything in my Pipeline YAML file in order to access the value of MY_SECRET in a Python script? Or is os.getenv('MY_SECRET') in my Python script enough to access that value?


Answers:
author: Syahrul
body: G'day, [@Sharon Machlis](/t5/user/viewprofilepage/user-id/5150318)

Welcome to the community!

Each programming language has its own methods or modules that allow interaction with environment variables. This includes reading the value of a variable or writing to a variable. Specifically for Python, the environment variables are usually accessed using the os module. You just need to import this module into your script and then use the [os.environ](https://docs.python.org/3/library/os.html#os.environ) function to interact with the variables.

Example on how you can reference the variables :  

```
import os# Set environment variablesos.environ['API_USER'] = 'username'os.environ['API_PASSWORD'] = 'secret'# Get environment variablesUSER = os.getenv('API_USER')PASSWORD = os.environ.get('API_PASSWORD')# Getting non-existent keysFOO = os.getenv('FOO') # NoneBAR = os.environ.get('BAR') # NoneBAZ = os.environ['BAZ'] # KeyError: key does not exist.
```

I would also recommend checking the following documentation on [How to set and get environment variables in Python](https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5)

Regards,  
Syahrul

comments: author: Sharon Machlis
body: Thank you, [@Syahrul](/t5/user/viewprofilepage/user-id/744536)! I'm familiar with how to access environment variables in Python either with os.getenv() or by reading a .env file or by exporting the values into my current working environment. My problem is that I don't know how BitBucket secrets work. Once I set my secret in BitBucket Cloud by going to Repository settings \> \[under the Pipelines section\] Repository Variables and defining a variable, does that mean it is automatically an environment variable and I can access it with os.getenv('MY_SECRET')? Or I need to use os.environ.get('MY_SECRET')? Or something else? Because os.getenv('MY_SECRET') is not working in my script, my script is failing.


author: Syahrul
body: Hey [@Sharon Machlis](/t5/user/viewprofilepage/user-id/5150318)

Thanks for clarifying.

What you understand about variables is correct. Once the variables are added to the workspace, repository, or deployment, they are accessible in your pipeline build, and you can call them according to their variable names.

So in your use case, you add them to the repository variable as HOME as 123 and use the following example:

```
import os

home = os.environ['HOME'] 
print("HOME:", home)
```

It should print out the variable as 123:  

    python3 https://bitbucket.org/m-syahrul/python-env/src/40112ffe3b703180cf1ceb4b35ad2f99925c05dd/variable.pyHOME: 123

Regards,  
Syahrul


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

secrets