Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 

Credit: @Vitaliy-R Your startup blogs kindled my interest to explore working with Python in SAP ecosystem. Python in BAS and Jupyter in BAS 

When I first started exploring SAP Business Application Studio (BAS), I was curious about how Python could fit into the SAP landscape. I’ve mostly associated BAS with HANA artefacts(SQLScript, hdbcalculationview, hdbreptask etc.) and CAP artefacts, so writing a Python script inside BAS felt like venturing into new territory. My goal was simple: write a basic script and connect it to SAP HANA Cloud. What I discovered along the way is that Python not only works smoothly in BAS but also makes it easy to interact with HANA Cloud, opening up opportunities for data exploration, automation, and integration in a way that feels both modern and approachable.

Before jumping into the Python script, I had to get my environment ready in SAP Business Application Studio (BAS). Here’s what I set up:

A BAS dev space with a full-stack cloud application space since it supports multiple runtimes, including Python. I had a space with HANA Native Application type. Since the Python tools extension is not added by default, I edited the space to select the Python tools in the additional extension options. 

HANA Dev Space Python extension

 Note: For initial steps to check the Python version, Jupyter notebook and set ups refer to the blogs listed at the start. 

Use Case: I attempted to achieve the following: 

  • Establish a connection to HANA Cloud
  • Execute an SQL query on a table/view 
  • Display the results

In the BAS, I created a project from Template: SAP HANA Database Project

Project Template.png

 

Next step: Create a notebook file. 

notebook file.png

My guide to connect to HANA Cloud: Connect to HANA Cloud 

When I first tried importing hdbcli into my Jupyter Notebook within BAS, I ran into the same ModuleNotFoundError. Even though I had already installed hdbcli In the terminal, the notebook kernel wasn’t recognizing it. On some search and prompting with GPT( 😁), I understood that it's a common issue because Jupyter can run in a different Python environment than the terminal. The fix was simple: I ran

import sys
!{sys.executable} -m pip install hdbcli

directly in a notebook cell. This ensures that the HANA client is installed in the same environment as the notebook kernel. After this step, I could successfully import dbapi and connect to HANA Cloud without any errors. It was a small but important lesson about Python environments in BAS, especially when using Jupyter.

hdbcli Module Not found.pngWith the hdbcli package installed and working in my Jupyter Notebook, I was ready to write my first Python script to connect to SAP HANA Cloud.

In the next cell, I imported hdbcli in this notebook. 

import hdbcli
print(hdbcli.__file__)

 import hdbcli.png

 The next step was to gain access to the dbapi interface, which allows you to establish connections, execute SQL queries, and fetch results from your HANA Cloud instance. This simple import is the gateway to working with HANA directly from Python.

from hdbcli import dbapi

The next step is to establish a connection to your HANA Cloud instance. This requires specifying the host, port, username, and password.

hana cloud connection.png

 After connecting, you can create a cursor object to execute SQL statements. An SQL statement, preferably a Select Query to test the retrieval of data from HANA Cloud. In my case, I used a Select with count on the number of records in a view. Once the variables were ready, execute the connection cursor object.

Note: in the SQL variable, use single quotes and a semicolon at the end of the query. (beginner tip 🙂 )

Execution Cursor.png

Now is the time to test the data retrieval from the script and compare it with the Database Explorer.

Drum roll....🥁

Data in DB explorer.png

 

Data in Script.png

 

 

 

 

 

 

 

Hurray 🎉

Completing my first Python script in SAP Business Application Studio and connecting it to HANA Cloud was an exciting milestone. From the initial curiosity to the small hurdles like installing hdbcli in the notebook and finally seeing my script return results, every step felt like a mini victory.

That simple output from HANA Cloud made all the effort worthwhile and gave me a real sense of accomplishment.

This experience has sparked my curiosity to explore more complex queries, data analysis, and automation using Python in SAP.

I hope my journey inspires others to take that first step and discover how fun and powerful working with Python and HANA Cloud can be.

Chao. 

Labels in this area