6

Persisting Python environment when using Jupyter in SAP Business Application Stu...

 11 months ago
source link: https://blogs.sap.com/2023/06/01/persisting-python-environment-when-using-jupyter-in-sap-business-application-studio/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
June 1, 2023 3 minute read

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'))"
2023-06-01_18-26-30.png

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…

2023-06-01_19-04-57.png

…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
pwd
2023-06-01_19-12-18.png

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
2023-06-01_19-48-40.png

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'))"
2023-06-01_19-53-20.png

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
2023-06-01_20-00-39.png

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: https://help.sap.com/doc/cd94b08fe2e041c2ba778374572ddba9/2023_1_QRC/en-US/Installation.html#installation-guide

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

python -m pip show hana-ml
2023-06-01_20-28-21.png

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”…

2023-06-01_20-31-44.png

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

2023-06-01_20-34-00.png

… to open a project.

Then open the Command Pallet…

2023-06-01_20-37-18.png

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

2023-06-01_20-41-39.png

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

2023-06-01_20-46-56.png

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

2023-06-01_20-50-21.png

Run simple cells to validate

import hana_ml
hana_ml.__version__
2023-06-01_20-58-10.png

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 https://github.com/github/gitignore/blob/main/Python.gitignore:

2023-06-01_21-07-44.png

Restart your BAS Dev Space…

…and everything should keep working in Jupyter!


Regards,
-Vitaliy, aka @Sygyzmundovych


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK