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

Some time ago, my teammate @ajmaradiaga published a post about Running a Jupyter notebook in SAP Business Application Studio with packages, like Python Machine Learning Client for SAP HANA.

If you used that approach, then you might find that by default, all Python packages are installed to the site locations of the Python interpreter.

If the setting python.globalModuleInstallation is set...
VitaliyR_0-1753984525821.png

...then installed packages are going to a site packages location like /home/user/.asdf-inst/installs/python/3.13.1/lib/python3.13/site-packages, as can be checked with the following code for installation paths or for site packages:

pip list -v
python3 -c "import sysconfig; print(sysconfig.get_scheme_names())"
python3 -c "import sysconfig; print(sysconfig.get_paths('posix_prefix'))"
python3 -c "import sysconfig; print(sysconfig.get_path('purelib', 'posix_prefix'))"
python3 -c "import site; print(site.getsitepackages())"

VitaliyR_0-1753277248366.png

The problem with these locations is that they are not persisted between restarts of your BAS DevSpace.

So, you need to run pip install... again next time you start your DevSpace.

Use a Python virtual environment

The way I approach this in a DevSpace, assuming Jupyter and Python extensions are installed...
VitaliyR_0-1753278414290.png


...is by using Python's virtual environment for my Jupyter project.

Let's say my project in BAS is called hanaml_demo, ie. it is located in the directory /home/user/projects/hanaml_demo.

mkdir ~/projects/hanaml_demo && cd ~/projects/hanaml_demo

pwd

Create a virtual environment with venv

The built-in venv module in Python provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories.

python3 -m venv env --upgrade-deps
source env/bin/activate
type python

VitaliyR_1-1753278618535.png

Now -- that virtual environment env is activated -- you should install Python packages into the virtual environment location, not into the global one.

python3 -c "import site; print(site.getsitepackages())"

VitaliyR_2-1753279156081.png

Install the ipykernel package

As Antonio mentioned in his post, you will need to install the ipykernel package to run Python code using Jupyter in SAP Business Application Studio.

python -m pip install ipykernel

Install hana-ml or other required packages

In my case, I want to use a Python Machine Learning Client for SAP HANA, for which I need to install hana-ml and maybe some other packages depending on which functionality I plan to use: https://help.sap.com/doc/cd94b08fe2e041c2ba778374572ddba9/latest/en-US/Installation.html#installatio....

python -m pip install hana-ml
python -m pip show hana-ml

VitaliyR_3-1753280106150.png

Start Jupyter
and set the kernel to Python interpreter
from the virtual environment

Now, in SAP Business Application Studio, click on Explorer in the Activity tab and then on the "Open Folder" button.

... and choose a folder with a project that has the virtual environment included...

... to open a project.

Then open the Command Palette...

...and find+execute a command to create a new Jupyter notebook.

Select a kernel from your env Python's virtual environment.
VitaliyR_4-1753281648590.png

Run simple cells to validate

import hana_ml

hana_ml.__version__

VitaliyR_5-1753281878628.png

Let's save this notebook as test.ipynb.

Using Git?
Do not forget about the .gitignore file!

You can add env to your .gitignore, or just simply use one of the prepared templates, like https://github.com/github/gitignore/blob/main/Python.gitignore:

Restart your BAS Dev Space...

...and everything should keep working in Jupyter!


Regards,
-Vitaliy, aka @Sygyzmundovych

7 Comments
Labels in this area