Skip to content
Snippets Groups Projects
Develop_and_publish_modul.ipynb 9.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • {
     "cells": [
      {
       "cell_type": "markdown",
       "id": "bbefaee0",
       "metadata": {},
       "source": [
        "# Ein Python-Modul entwicklen und veröffentlichen\n",
        "\n",
        "<br/>\n",
        "<br/>\n",
        "\n",
        "Dieses Notebook finden Sie hier: TBD\n",
        "\n",
        "<br/>\n",
        "\n",
        "![CC-BY-NC](https://scm.cms.hu-berlin.de/ibi/python/-/raw/master/img/cc-by-nc.png)\n",
        "\n",
        "Dieses Notebook ist als freies Werk unter der Lizenz [Creative Commons Attribution-NonCommercial 3.0 Unported](http://creativecommons.org/licenses/by-nc/3.0/) verfügbar. Sie dürfen die Inhalte kopieren, verteilen und verändern, solange Sie die Urheber nennen und sie nicht für kommerzielle Zwecke nutzen."
       ]
      },
      {
       "cell_type": "markdown",
       "id": "30526362",
       "metadata": {},
       "source": [
        "## Was ist ein Modul?\n",
        "\n",
        "- Ein Modul ist eine Datei, die Python-Definitionen und -Anweisungen beinhaltet. Oder anders gesagt: Jede Python-Datei ist ein Modul und der Name des Moduls ist der Dateiname ohne die Endung `.py`\n",
        "- Ein Package ist eine Sammlung von Modulen. Hierfür liegen mehrere Module in einem Ordner, welcher auch eine Datei `__init__.py` enthalten muss. Dies unterscheidet ein Package von einem Ordner, der zufällig mehrere Module enthält."
       ]
      },
      {
       "cell_type": "markdown",
       "id": "edfaa8dd",
       "metadata": {},
       "source": [
        "## Module verwenden\n",
        "\n",
        "- Python wird mit einer Bibliothek von Standardmodulen ausgeliefert. Ein solches Modul kann mit dem Befehl `import` importiert und anschließend verwendet weden."
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "0741b008",
       "metadata": {},
       "outputs": [],
       "source": [
        "import json"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "af33edfc",
       "metadata": {},
       "source": [
        "- Wenn wir nun versuchen ein Modul zu installieren, welches nicht in den Standardmodulen enthalten ist, dann kommt es zu einer Fehlermeldung."
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "7c60f1b4",
       "metadata": {},
       "outputs": [],
       "source": [
        "import pandas"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "de66aa56",
       "metadata": {},
       "source": [
        "```\n",
        "---------------------------------------------------------------------------\n",
        "ModuleNotFoundError                       Traceback (most recent call last)\n",
        "Cell In[1], line 1\n",
        "----> 1 import pandas\n",
        "\n",
        "ModuleNotFoundError: No module named 'pandas'\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "5e9d899a",
       "metadata": {},
       "source": [
        "## Module installieren\n",
        "\n",
        "- Module, die nicht in der Bibliothek von Standardmodulen enthalten sind, müssen vor dem Import installiert werden. Hierfür wird ein Paketverwaltungsprogramm verwendet. Dies ist im Normfall `pip`. Mit dem Befehl `pip install` können Pakete installiert werden."
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "53926ebe",
       "metadata": {},
       "outputs": [],
       "source": [
        "!pip install pandas"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "1db617a4",
       "metadata": {},
       "source": [
        "```\n",
        "Collecting pandas\n",
        "  Downloading pandas-1.5.2-cp39-cp39-macosx_10_9_x86_64.whl (12.1 MB)\n",
        "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.1/12.1 MB 11.5 MB/s eta 0:00:00m eta 0:00:010:01:01\n",
        "Collecting pytz>=2020.1\n",
        "  Downloading pytz-2022.6-py2.py3-none-any.whl (498 kB)\n",
        "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 498.1/498.1 kB 7.8 MB/s eta 0:00:00m eta 0:00:01\n",
        "Collecting numpy>=1.20.3\n",
        "  Downloading numpy-1.23.5-cp39-cp39-macosx_10_9_x86_64.whl (18.1 MB)\n",
        "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.1/18.1 MB 11.6 MB/s eta 0:00:00m eta 0:00:01[36m0:00:01\n",
        "Requirement already satisfied: python-dateutil>=2.8.1 in /Users/frede/Arbeit/repos/programmiertutorium/venv/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
        "Requirement already satisfied: six>=1.5 in /Users/frede/Arbeit/repos/programmiertutorium/venv/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n",
        "Installing collected packages: pytz, numpy, pandas\n",
        "Successfully installed numpy-1.23.5 pandas-1.5.2 pytz-2022.6\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "5bbb3631",
       "metadata": {},
       "source": [
        "## Doch was passiert hier eigentlich?"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "4578dfe2",
       "metadata": {},
       "source": [
        "### Python Package Index (PyPI)\n",
        "\n",
        "- `pip`lädt Pakete aus dem Python Package Index (PyPI).\n",
        "- PyPI ist ein Sofware-Vezeichnis der Programmiersprache Python.\n",
        "- PyPI umfasst knapp 420.00 Projekte (Stand: Dezember 2022)\n",
        "- Jede:r kann sich auf PyPI (https://pypi.org) registrieren und ein Projekt erstellen.\n",
        "\n",
        "<div class=\"alert alert-info\">\n",
        "<b>Hinweis</b> Der Begriff \"Package\" kann etwas verwirrend sein. PyPI spricht auch von Packages. Ein PyPI-Package kann ein einzelnes Python-Modul sein oder aber auch mehrere Python-Packages umfassen. \n",
        "</div>"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "e9e054ee",
       "metadata": {},
       "source": [
        "### PyPI Testumgebung\n",
        "\n",
        "- Für Testzwecke gibt es die PyPI Testumgebgung (https://test.pypi.org)\n",
        "- Diese funktioniert genau wie die reale Version und sollte verwendet werden um die eigene Konfiguration zu testen. "
       ]
      },
      {
       "cell_type": "markdown",
       "id": "7c602480",
       "metadata": {},
       "source": [
        "## Mein erstes PyPI (Test)-Package"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "bd3673fd",
       "metadata": {},
       "source": [
        "### Struktur\n",
        "\n",
        "- TODO: Add screenshot"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "e4db9691",
       "metadata": {},
       "source": [
        "### pyproject.toml\n",
        "\n",
        "```toml\n",
        "[build-system]\n",
        "requires = [\"setuptools>=61.0\"]\n",
        "build-backend = \"setuptools.build_meta\"\n",
        "\n",
        "[project]\n",
        "name = \"Mein_Projekt\"\n",
        "version = \"0.0.1\"\n",
        "authors = [\n",
        "    { name = \"Mein Name\", email = \"Mein_Name@example.com\" }\n",
        "]\n",
        "description = \"Eine kurze Beschreibung für Mein Projekt\"\n",
        "readme = \"README.md\"\n",
        "requires-python = \">=3.7\"\n",
        "\n",
        "classifiers = [\n",
        "    \"Programming Language :: Python :: 3\",\n",
        "    \"License :: OSI Approved :: MIT License\",\n",
        "    \"Operating System :: OS Independent\",\n",
        "]\n",
        "\n",
        "[project.urls]\n",
        "\"Homepage\" = \"https://...\"\n",
        "\"Bug Tracker\" = \"https://...\"\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "646e592e",
       "metadata": {},
       "source": [
        "### README.md\n",
        "\n",
        "```\n",
        "# Mein Projekt\n",
        "\n",
        "Dies ist ein Beispielprojekt.\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "de1fcd4f",
       "metadata": {},
       "source": [
        "## LICENSE\n",
        "\n",
        "```\n",
        "Apache License\n",
        "                           Version 2.0, January 2004\n",
        "                        http://www.apache.org/licenses/\n",
        "\n",
        "   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n",
        "\n",
        "   1. Definitions.\n",
        "\n",
        "      \"License\" shall mean the terms and conditions for use, reproduction,\n",
        "      and distribution as defined by Sections 1 through 9 of this document.\n",
        "\n",
        "      \"Licensor\" shall mean the copyright owner or entity authorized by\n",
        "      the copyright owner that is granting the License.\n",
        "\n",
        "      \"Legal Entity\" shall mean the union of the acting entity and all\n",
        "      other entities that control, are controlled by, or are under common\n",
        "      control with that entity. For the purposes of this definition,\n",
        "      \"control\" means (i) the power, direct or indirect, to cause the\n",
        "      direction or management of such entity, whether by contract or\n",
        "      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n",
        "      outstanding shares, or (iii) beneficial ownership of such entity.\n",
        "      \n",
        "      [...]\n",
        "\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "86650a5a",
       "metadata": {},
       "source": [
        "### Package für den Upload erzeugen\n",
        "\n",
        "```\n",
        "python3 -m pip install --upgrade build\n",
        "python3 -m build\n",
        "```"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "300f78f8",
       "metadata": {},
       "source": [
        "### Package auf Testserver hochladen\n",
        "\n",
        "```\n",
        "python3 -m pip install --upgrade twine\n",
        "python3 -m twine upload --repository testpypi dist/*\n",
        "```"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "9ebcc1d5",
       "metadata": {},
       "outputs": [],
       "source": []
      }
     ],
     "metadata": {
      "language_info": {
       "name": "python",
       "pygments_lexer": "ipython3"
      }
     },
     "nbformat": 4,
     "nbformat_minor": 5
    }