From a983678f63c5fcee7bfa06717dbef15e4a480d17 Mon Sep 17 00:00:00 2001
From: Daniel M <daniel.q.mueller@stud.h-da.de>
Date: Mon, 8 Nov 2021 13:52:31 +0100
Subject: [PATCH] Install into venv instead of system

All mp packages and also jupyter are now installed into a venv.
This should avoid conflicts with global python packages.
---
 roles/jupyter-lab/files/jupyter.service       |  3 +-
 roles/jupyter-lab/tasks/main.yml              |  5 ++--
 .../ml-python-packages/files/requirements.txt |  6 ++++
 roles/ml-python-packages/tasks/main.yml       | 30 +++++++++----------
 4 files changed, 24 insertions(+), 20 deletions(-)
 create mode 100644 roles/ml-python-packages/files/requirements.txt

diff --git a/roles/jupyter-lab/files/jupyter.service b/roles/jupyter-lab/files/jupyter.service
index cd84905..bdbf447 100644
--- a/roles/jupyter-lab/files/jupyter.service
+++ b/roles/jupyter-lab/files/jupyter.service
@@ -5,8 +5,7 @@ Description=Jupyter Lab
 Type=simple
 PIDFile=/run/jupyter.pid
 # Password: [empty string]
-ExecStart=/usr/bin/python3 -m jupyterlab --ip="0.0.0.0"
---notebook-dir=/home/{{ target_user }}/notebooks --no-browser --NotebookApp.password='sha1:9a2d316959ac:843b251c27024afb46174ce40ce0ebebcf29217b'
+ExecStart=/home/{{ target_user }}/.ml-venv/bin/jupyter-lab --ip="0.0.0.0" --notebook-dir=/home/{{ target_user }}/notebooks --no-browser --NotebookApp.password='sha1:9a2d316959ac:843b251c27024afb46174ce40ce0ebebcf29217b'
 User={{ target_user }}
 Group={{ target_user }}
 Restart=always
diff --git a/roles/jupyter-lab/tasks/main.yml b/roles/jupyter-lab/tasks/main.yml
index adb63c1..13f4c1b 100644
--- a/roles/jupyter-lab/tasks/main.yml
+++ b/roles/jupyter-lab/tasks/main.yml
@@ -19,9 +19,9 @@
     update_cache: true
 
 - name: Install jupyter-lab
-  become: true
   pip:
-    executable: pip3
+    virtualenv_command: /usr/bin/python3 -m venv
+    virtualenv: "/home/{{ target_user }}/.ml-venv"
     name: jupyterlab
     state: present
 
@@ -39,3 +39,4 @@
     state: started
     enabled: true
     daemon_reload: true
+
diff --git a/roles/ml-python-packages/files/requirements.txt b/roles/ml-python-packages/files/requirements.txt
new file mode 100644
index 0000000..4a89ed4
--- /dev/null
+++ b/roles/ml-python-packages/files/requirements.txt
@@ -0,0 +1,6 @@
+numpy==1.21.*
+matplotlib==3.4.*
+pandas==1.3.*
+Pillow==8.4.*
+scikit-learn==1.0.*
+
diff --git a/roles/ml-python-packages/tasks/main.yml b/roles/ml-python-packages/tasks/main.yml
index 359f487..ee1c3cf 100644
--- a/roles/ml-python-packages/tasks/main.yml
+++ b/roles/ml-python-packages/tasks/main.yml
@@ -12,25 +12,23 @@
     state: present
     update_cache: true
 
-- name: Remove conflicting apt packages
-  become: true
-  apt:
-    name:
-      - python3-pil
-      - python3-numpy
-    state: absent
+- name: Copy requirements.txt to remote fs
+  copy:
+    src: files/requirements.txt
+    dest: /tmp/ml-requirements.txt
 
 - name: Install ml python packages
-  become: true
   pip:
-    executable: pip3
-    name:
-      - numpy
-      - sklearn
-      - matplotlib
-      - pandas
-      - Pillow
+    virtualenv_command: /usr/bin/python3 -m venv
+    requirements: /tmp/ml-requirements.txt
+    virtualenv: "/home/{{ target_user }}/.ml-venv"
+    # name:
+      # - numpy
+      # - sklearn
+      # - matplotlib
+      # - pandas
+      # - Pillow
       # - torch
       # - torchvision
       # - fastai
-    state: present
+    # state: present
-- 
GitLab