Skip to content

Python3 - virtuelle Entwicklungsumgebung

Angeheftet Python3
  • Ich hatte da doch mal wieder ein kleines Problem, was ich mir nicht erklären konnte. Wenn ich als User mein Projekt geöffnet hatte, ging das setStyleSheet der Buttons nicht.

    self.button2 = QPushButton("Add Backup", self)
    self.button2.clicked.connect(self.close)
    self.button2.clicked.connect(add_backup)
    

    Als User Root aber schon 🤔 Da fiel mir ein, das in einem Buch davon die Rede war, das man Projekte in virtuellen Entwicklungsumgebungen erstellt um das alles vom Hauptsystem zu trennen. Ich hoffe mal, das stimmt bis hierher....

    Für Python3 braucht man folgende Tools

    sudo apt install python3 python3-dev python3-venv
    

    python3-venv war bei mir noch nicht installiert. Also mal nachgeholt.

    venv erstellen

    Verwenden Sie den Befehl venv, um eine virtuelle Kopie der gesamten Python-Installation zu erstellen. In dieser Anleitung wird eine virtuelle Kopie in einem Ordner namens venv erstellt. Sie können jedoch einen beliebigen Namen für den Ordner angeben.

    cd projekt
    python3 -m venv venv
    

    Danach hat man im Projektordner einen Ordner namens venv.

    venv aktivieren

    source venv/bin/activate
    

    Jetzt können Sie Pakete installieren, ohne dass dies Auswirkungen auf andere Projekte oder Ihre globale Python-Installation hat:

    venv deaktivieren

    deactivate
    

    Eine venv Umgebung erkennt man auf der Shell an dem (venv) am Anfang.

    (venv) frank@frank-MS-XXXX:~/restic_ui$ python3 restic_ui.py
    

    Und schwupps, ein Pakete neu installiert und danach waren auch die Buttons bunt 🙂

    Quelle: https://cloud.google.com/python/setup#linux

  • FrankMF FrankM hat dieses Thema am angepinnt
  • Nach meinem Umzug auf Ubuntu 21.04 ging mein Restic UI nicht mehr in der virtuellen Umgebung. Hier kurz mein Script, wie ich das üblicherweise starte.

    cd /home/frank/Restic_UI_Produktiv/restic-ui-public
    git stash
    git pull
    cp /home/frank/backup_list.json /home/frank/Restic_UI_Produktiv/restic-ui-public/
    source venv/bin/activate
    python3 restic_ui.py
    

    Ich habe eine eigene Backupliste, die ich beim Starten kopieren. Ok, das ging so aber nicht. Ich bekam immer folgendes.

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ python3 restic_ui.py
    Traceback (most recent call last):
          File "/home/frank/Restic_UI_Produktiv/restic-ui-public/restic_ui.py", line 24, in <module>
            from PyQt5.QtWidgets import(
        ModuleNotFoundError: No module named 'PyQt5'
    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install PyQt5
        Traceback (most recent call last):
          File "/home/frank/restic_ui/venv/bin/pip3", line 5, in <module>
            from pip._internal.cli.main import main
        ModuleNotFoundError: No module named 'pip'
    

    Ok, sieht nicht so gut aus. Nachdenken 🤔

    Was passiert, wenn ich eine virtuelle Umgebung anlege? So genau weiß ich das auch nicht, aber im Projekt wird ein Ordner venv angelegt. Wenn ich mich recht erinnere, habe ich gelesen, das man nun alles mögliche mittels pip installieren kann, dieses passiert aber nur in der virtuellen Umgebung und nicht im echten System. (Hoffe das stimmt)

    Jetzt hatte ich den Ordner ja vom alten System und die Daten in venv dürften nicht stimmen, war meine Vermutung. Also ausprobieren. Den Ordner venv mal an einen anderen Ort verschoben, so zur Sicherung. Dann die virtuelle Umgebung neu installiert.

    python3 -m venv venv
    source venv/bin/activate
    

    Programm gestartet.

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ python3 restic_ui.py  
    Traceback (most recent call last):
          File "/home/frank/Restic_UI_Produktiv/restic-ui-public/restic_ui.py", line 24, in <module>
            from PyQt5.QtWidgets import(
        ModuleNotFoundError: No module named 'PyQt5'
    

    Ok, er meckert über ein fehlendes Modul, aber diesmal geht das Installieren 🙂

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install PyQt5
    Collecting PyQt5
      Using cached PyQt5-5.15.4-cp36.cp37.cp38.cp39-abi3-manylinux2014_x86_64.whl (8.3 MB)
    Collecting PyQt5-Qt5>=5.15
      Using cached PyQt5_Qt5-5.15.2-py3-none-manylinux2014_x86_64.whl (59.9 MB)
    Collecting PyQt5-sip<13,>=12.8
      Using cached PyQt5_sip-12.9.0-cp39-cp39-manylinux1_x86_64.whl (328 kB)
    Installing collected packages: PyQt5-sip, PyQt5-Qt5, PyQt5
    Successfully installed PyQt5-5.15.4 PyQt5-Qt5-5.15.2 PyQt5-sip-12.9.0
    

    Programm gestartet, alles wieder gut 🤓

  • Nach Systemwechsel erneut Probleme. Hier noch mal aufgelistet.

    Aufpassen, das kein venv Ordner vorhanden ist! Neu anlegen!

    python3 -m venv venv
    

    Dann meckert mein Linux Mint Cinnamon

    The virtual environment was not created successfully because ensurepip is not
    available.  On Debian/Ubuntu systems, you need to install the python3-venv
    package using the following command.
    
        apt install python3.8-venv
    
    You may need to use sudo with that command.  After installing the python3-venv
    package, recreate your virtual environment.
    
    Failing command: ['/home/frank/Restic_UI_Produktiv/restic-ui-public/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
    

    Ok, das ist einfach 😉

    apt install python3.8-venv
    python3 -m venv venv
    

    Danach aktivieren

    source venv/bin/activate
    

    Installation von PyQt5

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install PyQt5
    Collecting PyQt5
      Downloading PyQt5-5.15.4-cp36.cp37.cp38.cp39-abi3-manylinux2014_x86_64.whl (8.3 MB)
         |████████████████████████████████| 8.3 MB 1.7 MB/s 
    Collecting PyQt5-Qt5>=5.15
      Downloading PyQt5_Qt5-5.15.2-py3-none-manylinux2014_x86_64.whl (59.9 MB)
         |████████████████████████████████| 59.9 MB 18.3 MB/s 
    Collecting PyQt5-sip<13,>=12.8
      Downloading PyQt5_sip-12.9.0-cp38-cp38-manylinux1_x86_64.whl (332 kB)
         |████████████████████████████████| 332 kB 46.1 MB/s 
    Installing collected packages: PyQt5-Qt5, PyQt5-sip, PyQt5
    Successfully installed PyQt5-5.15.4 PyQt5-Qt5-5.15.2 PyQt5-sip-12.9.0
    

    Danach meckert er über ein fehlendes Modul requests

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ python3 restic_ui.py
    Traceback (most recent call last):
      File "restic_ui.py", line 41, in <module>
        from functions import (
      File "/home/frank/Restic_UI_Produktiv/restic-ui-public/functions.py", line 19, in <module>
        import requests as req
    ModuleNotFoundError: No module named 'requests'
    

    Installation requests

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install requests
    Collecting requests
      Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
         |████████████████████████████████| 61 kB 802 kB/s 
    Collecting chardet<5,>=3.0.2
      Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
         |████████████████████████████████| 178 kB 2.7 MB/s 
    Collecting certifi>=2017.4.17
      Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
         |████████████████████████████████| 145 kB 14.0 MB/s 
    Collecting idna<3,>=2.5
      Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
         |████████████████████████████████| 58 kB 9.4 MB/s 
    Collecting urllib3<1.27,>=1.21.1
      Downloading urllib3-1.26.6-py2.py3-none-any.whl (138 kB)
         |████████████████████████████████| 138 kB 11.5 MB/s 
    Installing collected packages: chardet, certifi, idna, urllib3, requests
    Successfully installed certifi-2021.5.30 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.6
    

    Und schwupps, geht mein Tool wieder 🙂

    4968ce42-7c66-4c8a-a2ad-424b9a529d87-grafik.png

  • Python - Formatumwandlung str -> float

    Python3
    1
    0 Stimmen
    1 Beiträge
    160 Aufrufe
    Niemand hat geantwortet
  • Python - Dict -> JSON und umgekehrt

    Python3
    1
    0 Stimmen
    1 Beiträge
    191 Aufrufe
    Niemand hat geantwortet
  • Python - Match-Case Statement

    Python3
    1
    0 Stimmen
    1 Beiträge
    83 Aufrufe
    Niemand hat geantwortet
  • Python3 - enumerate()

    Python3
    2
    0 Stimmen
    2 Beiträge
    88 Aufrufe
    FrankMF

    Ich habe noch was Code gefunden, den ich mal dringend aufräumen musste 🙂 Wenn ich mit dem Restic UI die Snapshots anzeige, dann sieht das so aus.

    reading repository password from stdin ID Time Host Tags Paths ---------------------------------------------------------------------------- 4e769748 2021-12-31 21:48:34 frank-MS-7C37 /home/frank/Bilder 34298934 2022-01-26 20:15:53 frank-MS-7C37 /home/frank/Bilder 0d5d88f2 2022-01-27 21:05:07 frank-MS-7C37 /home/frank/Bilder f0e7d5a7 2022-01-29 13:46:12 frank-MS-7C37 /home/frank/Bilder 79918d70 2022-01-29 13:52:56 frank-MS-7C37 /home/frank/Bilder d74272a3 2022-01-30 11:54:20 frank-MS-7C37 /home/frank/Bilder 11b0b5ad 2022-01-30 11:57:44 frank-MS-7C37 /home/frank/Bilder 4a7450d2 2022-02-20 09:35:56 frank-MS-7C37 /home/frank/Bilder c1a4a51d 2022-02-20 09:36:43 frank-MS-7C37 /home/frank/Bilder d178ded5 2022-02-20 09:38:53 frank-MS-7C37 /home/frank/Bilder 997e2259 2022-02-20 16:35:57 frank-MS-7C37 /home/frank/Bilder 3320ad5c 2022-02-20 16:49:19 frank-MS-7C37 /home/frank/Bilder ---------------------------------------------------------------------------- 12 snapshots

    Mich interessieren die IDs, weil ich bei der Eingabe der IDs überprüfen möchte, ob die eingegebene ID existiert. Also muss ich das irgendwie auslesen und speichern. Das habe ich bis jetzt so gemacht.

    def id_append(self, result): # We split the result into individual lines. # Result was passed. And create an list. result_list = result.split('\n') # We count the number of repositories and search for 'source'. count = result.count(backup_data[row].source) # Offset added, first interesting line is line 3! count = count + 3 # We loop through the list and output the ID's and store in snapshot_id x = 3 while x < count: a = result_list[x] y = a[0:8] snapshot_id.append(y) x = x + 1

    Das erschien mir heute, beim erneuten Betrachten, Spaghetticode zu sein. Machen wir das mal etwas ordentlicher.

    Ich nehme die Ausgabe (stdout) und erzeuge eine Liste, jede Zeile ein Element.

    result_list = result.split('\n')

    Sieht so aus

    ['reading repository password from stdin', 'ID Time Host Tags Paths', '----------------------------------------------------------------------------', '4e769748 2021-12-31 21:48:34 frank-MS-7C37 /home/frank/Bilder', '34298934 2022-01-26 20:15:53 frank-MS-7C37 /home/frank/Bilder', '0d5d88f2 2022-01-27 21:05:07 frank-MS-7C37 /home/frank/Bilder', 'f0e7d5a7 2022-01-29 13:46:12 frank-MS-7C37 /home/frank/Bilder', '79918d70 2022-01-29 13:52:56 frank-MS-7C37 /home/frank/Bilder', 'd74272a3 2022-01-30 11:54:20 frank-MS-7C37 /home/frank/Bilder', '11b0b5ad 2022-01-30 11:57:44 frank-MS-7C37 /home/frank/Bilder', '4a7450d2 2022-02-20 09:35:56 frank-MS-7C37 /home/frank/Bilder', 'c1a4a51d 2022-02-20 09:36:43 frank-MS-7C37 /home/frank/Bilder', 'd178ded5 2022-02-20 09:38:53 frank-MS-7C37 /home/frank/Bilder', '997e2259 2022-02-20 16:35:57 frank-MS-7C37 /home/frank/Bilder', '3320ad5c 2022-02-20 16:49:19 frank-MS-7C37 /home/frank/Bilder', '----------------------------------------------------------------------------', '12 snapshots', '']

    Diesmal benutzen wir die enumerate() Funktion

    for count, value in enumerate(result_list): print(count, value)

    Das was raus kommt, sieht so aus

    0 reading repository password from stdin 1 ID Time Host Tags Paths 2 ---------------------------------------------------------------------------- 3 4e769748 2021-12-31 21:48:34 frank-MS-7C37 /home/frank/Bilder 4 34298934 2022-01-26 20:15:53 frank-MS-7C37 /home/frank/Bilder 5 0d5d88f2 2022-01-27 21:05:07 frank-MS-7C37 /home/frank/Bilder 6 f0e7d5a7 2022-01-29 13:46:12 frank-MS-7C37 /home/frank/Bilder 7 79918d70 2022-01-29 13:52:56 frank-MS-7C37 /home/frank/Bilder 8 d74272a3 2022-01-30 11:54:20 frank-MS-7C37 /home/frank/Bilder 9 11b0b5ad 2022-01-30 11:57:44 frank-MS-7C37 /home/frank/Bilder 10 4a7450d2 2022-02-20 09:35:56 frank-MS-7C37 /home/frank/Bilder 11 c1a4a51d 2022-02-20 09:36:43 frank-MS-7C37 /home/frank/Bilder 12 d178ded5 2022-02-20 09:38:53 frank-MS-7C37 /home/frank/Bilder 13 997e2259 2022-02-20 16:35:57 frank-MS-7C37 /home/frank/Bilder 14 3320ad5c 2022-02-20 16:49:19 frank-MS-7C37 /home/frank/Bilder 15 ---------------------------------------------------------------------------- 16 12 snapshots 17

    Mich interessieren nur die Zeilen mit /home/frank/Bilder Das kann man so abfragen

    if backup_data[row].source in value:

    Und jetzt nur die ID entsprechend abspeichern

    snapshot_id.append(value[0:8])

    Sieht jetzt komplett so aus

    result_list = result.split('\n') for count, value in enumerate(result_list): if backup_data[row].source in value: snapshot_id.append(value[0:8]) print(count, value[0:8])

    Somit speichert er jetzt nur noch die IDs, der folgenden Ausgabe.

    3 4e769748 4 34298934 5 0d5d88f2 6 f0e7d5a7 7 79918d70 8 d74272a3 9 11b0b5ad 10 4a7450d2 11 c1a4a51d 12 d178ded5 13 997e2259 14 3320ad5c

    Diese IDs, kann ich dann weiterverarbeiten.

  • 0 Stimmen
    2 Beiträge
    265 Aufrufe
    FrankMF

    Ich ko... immer, wenn ich mein VSCodium neu installieren muss. Das größte Problem dabei ist immer, das ich fast immer vergesse den Python Interpreter zu setzen.

    "CTRL+SHIFT+P" and choose the correct python interpreter.

    46f85075-4dbc-414c-94c2-abc5bb6009b6-grafik.png

    Danach startet das Python Programm auch wieder aus der richtigen Entwicklungsumgebung 🙂

  • Python3 - Struktur eines Projektes

    Python3
    1
    0 Stimmen
    1 Beiträge
    108 Aufrufe
    Niemand hat geantwortet
  • Python3 - Globale Variablen

    Python3
    1
    0 Stimmen
    1 Beiträge
    128 Aufrufe
    Niemand hat geantwortet
  • Python und GUI

    Verschoben Python3
    1
    0 Stimmen
    1 Beiträge
    155 Aufrufe
    Niemand hat geantwortet