There is a lot of information available about creating and debugging Node.js services in SAP BTP. However, in some cases, we might need to use Python instead — for example, for tasks involving AI or data processing.
In this article, I’ll show you how to debug a Python-based SAP BTP service using VS Code (Visual Studio Code) locally.
Create an SAP BTP Trial Account. Please follow this tutorial for detailed information.
Add HANA to your SAP BTP trial account if you haven’t already.
To do this, refer to the section ‘Create an SAP HANA Cloud Instance’ of this tutorial.
Note: One of the steps in the tutorial above requires the Space ID.
If you're not sure where to find it, navigate to your space in the SAP BTP Cockpit and copy the Space ID from the URL.
Install Visual Studio Code (VS Code). You can download it from this page.
Install the SAP Cloud Foundry CLI. Refer to this guide on installing the cf CLI for detailed instructions.
Install the Python interpreter. Version 3.13 or higher is recommended.
Install the necessary extensions for VS Code:
Note: When installing the first extension (Python), the second one (Python Debugger) may be installed automatically. If not, you can install it manually.
Our service will not include authorization for the sake of simplicity, but it will use an SAP HANA service to demonstrate how to call a BTP service from the local environment. We will use the initial setup from this tutorial.
In the following sections, I will replicate some of the steps from that tutorial in this blog post, with a few minor adjustments.
---
applications:
- name: python-debug
random-route: true
path: ./
memory: 128M
buildpacks:
- python_buildpack
command: python server.py
services:
- python-debug-hana
IMPORTANT: Make sure you don’t already have another application named python-debug in your space. If you do, use a different name and update the entire tutorial accordingly.
python-3.13.x
Flask==2.3.*
cfenv==0.5.3
hdbcli==2.17.*
import os
from flask import Flask
from cfenv import AppEnv
from hdbcli import dbapi
app = Flask(__name__)
env = AppEnv()
hana_service = 'hana'
hana = env.get_service(label=hana_service)
port = int(os.environ.get('PORT', 3000))
@app.route('/')
def hello():
if hana is None:
return "Can't connect to HANA service '{}' – check service name?".format(hana_service)
else:
conn = dbapi.connect(address=hana.credentials['host'],
port=int(hana.credentials['port']),
user=hana.credentials['user'],
password=hana.credentials['password'],
encrypt='true',
sslTrustStore=hana.credentials['certificate'])
cursor = conn.cursor()
cursor.execute("select CURRENT_UTCTIMESTAMP from DUMMY")
ro = cursor.fetchone()
cursor.close()
conn.close()
return "Current time is: " + str(ro["CURRENT_UTCTIMESTAMP"])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
This is a simple server that will return the result of a basic HANA SQL query when requested (in our case, the current UTC time).
cf login
When prompted, enter your API endpoint. You can find it on the Account Overview page in your SAP BTP account.
Also, enter the registration email and password for your SAP BTP trial account.
cf create-service hana hdi-shared python-debug-hana
cf push
Note: If you make any changes to the service file and want to deploy again, simply run the ‘cf push’ command again.
Local debugging of applications running on SAP Business Technology Platform (SAP BTP) is not only possible, but also practical in many cases. Here’s why:
Thanks to these two factors, local development and debugging in SAP BTP is straightforward. It enables faster development cycles and easier troubleshooting without the need to redeploy for every change.
To enable local debugging of a Python-based service, follow these steps:
import os
from flask import Flask
from cfenv import AppEnv
from hdbcli import dbapi
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
env = AppEnv()
hana_service = 'hana'
hana = env.get_service(label=hana_service)
port = int(os.environ.get('PORT', 3000))
@app.route('/')
def hello():
if hana is None:
return "Can't connect to HANA service '{}' – check service name?".format(hana_service)
else:
conn = dbapi.connect(address=hana.credentials['host'],
port=int(hana.credentials['port']),
user=hana.credentials['user'],
password=hana.credentials['password'],
encrypt='true',
sslTrustStore=hana.credentials['certificate'])
cursor = conn.cursor()
cursor.execute("select CURRENT_UTCTIMESTAMP from DUMMY")
ro = cursor.fetchone()
cursor.close()
conn.close()
return "Current time is: " + str(ro["CURRENT_UTCTIMESTAMP"])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
This new logic will let you automatically read the key-value pairs from your .env file and make them available via os.environ, just like in the cloud environment.
Flask==2.3.*
cfenv==0.5.3
hdbcli==2.17.*
python-dotenv
Every Python project should run in its own isolated environment to avoid dependency conflicts.
In VS Code, open the Command Palette (Ctrl+Shift+P), and choose ‘Python: Create Environment…’Note: You can also do this later using the command pip install -r requirements.txt.
cf env python-debug
Now, copy the contents of the VCAP_SERVICES JSON block to your clipboard.
VCAP_SERVICES=<minimized_json>
Note: If you have already created the configuration file, simply open it for edit.
http://127.0.0.1/
The current date and time from the HANA server will be displayed
Now, you can use all the Python debugger’s features to debug your program.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
8 | |
7 | |
6 | |
6 | |
4 | |
4 | |
4 | |
4 | |
4 |