Skip to Content
Technical Articles
Author's profile photo Witalij Rudnicki

Persisting Python environment when using Jupyter in SAP Business Application Studio

Some time ago my teammate Antonio Maradiaga 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 installed by you are going to a user location /home/user/.local/lib/python3.9/site-packages, as can be checked with the following commands:

python3 -c "import sysconfig; print(sysconfig.get_scheme_names())"
python3 -c "import sysconfig; print(sysconfig.get_paths('posix_user'))"
python3 -c "import sysconfig; print(sysconfig.get_path('purelib', 'posix_user'))"

The problem with this location is it is not persisted between restarts of your BAS DevSpace.

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

Use Python virtual environment

The way I approach this in a DevSpace, assuming Jupyter and Python extensions are installed…

…is to use 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

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
which python

Now — that virtual environment env is activated — you should install Python packages into the “home” location, not into the “user” one.

python3 -c "import sysconfig; print(sysconfig.get_path('purelib', 'posix_home'))"

Install ipykernel package

As Antonio mentioned in his post you will need to install ipykernel package to be able 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 like Python Machine Learning Client for SAP HANA for which I need to install hana-ml and some other packages depending on what functionality I plan to use:

python -m pip install hana-ml 'jinja2>=3' ipywidgets wordcloud 'plotly>=4.14.3' 'shapely>=1.7.1'

python -m pip show hana-ml

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

Now in SAP Business Application Studion click on Explorer in the Activity tab, and then on the button “Open Folder”…

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

… to open a project.

Then open the Command Pallet…

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

Select a kernel from your env Python’s virtual environment.

Once it is started you should see notifications about Jupyter kernel services started.

Run simple cells to validate

import hana_ml

Let’s save this notebook as test.ipynb.

Using Git?
Do not forget about .gitignore file!

You can add env to your .gitignore, or just simply use one of the prepared templates, like

Restart your BAS Dev Space…

…and everything should keep working in Jupyter!

-Vitaliy, aka @Sygyzmundovych

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Avital Margalit
      Avital Margalit

      Thank you Witalij Rudnicki, for sharing this useful blog.

      High productivity tools for Python development in SAP Business Application Studio is part of Q2 roadmap.



      Avital Margalit

      Product Owner, SAP Business Application Studio


      Author's profile photo Bhagabat Prasad Behera
      Bhagabat Prasad Behera

      Thank you Witalij for the great article.

      I tried all the steps however, I am not able to see the env kernel in the notebook. Could you please advise what could be the issue?

      Thanks, Bhagabat

      Author's profile photo Marcus Schiffer
      Marcus Schiffer

      Hi Bhagabat,


      could you solve this ?

      I seem to have the same issue.




      Author's profile photo Witalij Rudnicki
      Witalij Rudnicki
      Blog Post Author


      The fix was implemented last weekend, and I was able to select a kernel in Jupyter in BAS now.

      I hope it works now for you too.

      Sorry for the inconvenience and the delay,

      Author's profile photo Marcus Schiffer
      Marcus Schiffer



      same issue for me. No python kernel visible. Python 3.9.2 is installed in usr/bin. Tried to uninstall the extensions and reinstall etc. No success.

      What might be wrong ?

      Any help appreciated.




      Author's profile photo Witalij Rudnicki
      Witalij Rudnicki
      Blog Post Author

      Hi Marcus. The engineering team is aware of the issue, which was caused by the incompatibility of open-source components used. They are working on the solution, but I do not have information about the date.

      Sorry for the inconvenience.

      Author's profile photo Marcus Schiffer
      Marcus Schiffer

      Hi Witalij,

      thanks for clarification.

      I will have patience....