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 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.
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 pwd
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'))"
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
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/latest/en-US/Installation.html#installatio....
python -m pip install hana-ml 'jinja2>=3' ipywidgets wordcloud 'plotly>=4.14.3' 'shapely>=1.7.1' python -m pip show hana-ml
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.
import hana_ml hana_ml.__version__
Let's save this notebook as test.ipynb.
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:
...and everything should keep working in Jupyter!
Regards,
-Vitaliy, aka @Sygyzmundovych
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
18 | |
13 | |
12 | |
11 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 |