Skip to content

Commit

Permalink
Merge pull request #53 from minrk/package-tweaks
Browse files Browse the repository at this point in the history
minor tweaks to packaging
  • Loading branch information
minrk authored Nov 14, 2023
2 parents 1e891c1 + 844739a commit f610f88
Showing 1 changed file with 55 additions and 71 deletions.
126 changes: 55 additions & 71 deletions lectures/python/packages_and_testing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
{
"cell_type": "markdown",
"id": "a43bfb86",
"metadata": {},
"source": [
"## What makes `itertools` into a module and not a package?\n",
Expand Down Expand Up @@ -83,27 +84,43 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "ff72d629",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"def factorial(n: int) -> int:\n",
" \"\"\"Return the factorial of n, an exact integer >= 0.\n",
"\n",
" Args:\n",
" n (int): n!\n",
"\n",
" Returns:\n",
" int. The factorial value::\n",
"\n",
" >>> factorial(5)\n",
" 120\n",
" >>> factorial(0)\n",
" 1\n",
"\n",
" \"\"\"\n",
" if n == 0:\n",
" return 1\n",
" return n * factorial(n - 1)\n",
"\n",
"\n"
]
}
],
"source": [
"import os\n",
"\n",
"os.system(\"head -n 20 user_factorial.py\")"
"!head -n 20 user_factorial.py"
]
},
{
"cell_type": "markdown",
"id": "f758817d",
"metadata": {},
"source": [
"We can now import this function to our code by calling\n"
Expand Down Expand Up @@ -196,6 +213,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "39323d65",
"metadata": {},
"outputs": [
{
Expand All @@ -219,6 +237,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "ca6ba9df",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -368,6 +387,7 @@
{
"cell_type": "code",
"execution_count": 17,
"id": "f3d1a708",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -405,24 +425,29 @@
},
{
"cell_type": "markdown",
"id": "814cbc37",
"metadata": {},
"source": [
"We now consider each of the sections in this file in turn.\n",
"First we consider the `[...]`-notation. This defines a heading in a table, and we can create sub-tables, such as `[a]` and `[a.b]`.\n",
"Many Python packages support their own headings for configuring the repository, such as formatting, import sorting, type-checking etc.\n",
"\n",
"# build-system\n",
"### build-system\n",
"\n",
"Installing a package in Python means taking a set of files, and do some or all of the following options\n",
"- Compile files from foreign languages (such as C/C++)\n",
"- Move files from the current root directory to an appropriate path for the current installation of Python\n",
"We will use [setuptools](https://setuptools.pypa.io/) for this, a common Python packager.\n",
"\n",
"With the file above, we can now call the following (requires root access), if defaults to user installation (see below)"
"With the file above, we can now call the following.\n",
"Pip will always install in the _current Python environment_,\n",
"which may be system-wide (usually requires root permissions), per-user, or in a virtual environment."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c31b9e7",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -451,6 +476,7 @@
},
{
"cell_type": "markdown",
"id": "34c1dcf4",
"metadata": {},
"source": [
"We can check where the package now is located by calling"
Expand All @@ -459,63 +485,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['/home/dokken/.local/lib/python3.8/site-packages/pkg']\n"
]
}
],
"source": [
"!python3 -c \"import pkg; print(pkg.__path__)\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Non-root installation\n",
"We could have also installed the package in a few other ways. We could have used the `--user` flag to install it for the current user"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found existing installation: pkg 0.1.0\n",
"Uninstalling pkg-0.1.0:\n",
" Successfully uninstalled pkg-0.1.0\n",
"Processing ./examples/my-package\n",
" Installing build dependencies ... \u001b[?25ldone\n",
"\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n",
"\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25hRequirement already satisfied: numpy>=1.20.0 in /home/dokken/.local/lib/python3.8/site-packages (from pkg==0.1.0) (1.20.1)\n",
"Building wheels for collected packages: pkg\n",
" Building wheel for pkg (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for pkg: filename=pkg-0.1.0-py3-none-any.whl size=3855 sha256=74ffcb73548cc146aea86252c02514759097a4be3fca162753bdc9428bb57848\n",
" Stored in directory: /home/dokken/.cache/pip/wheels/55/cc/57/3120b08927ab03809b28e441ce1800d832c33b007a8f3fccf4\n",
"Successfully built pkg\n",
"Installing collected packages: pkg\n",
"Successfully installed pkg-0.1.0\n"
]
}
],
"source": [
"!python3 -m pip uninstall -y pkg\n",
"!python3 -m pip install --user ./examples/my-package"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b43920e8",
"metadata": {},
"outputs": [
{
Expand All @@ -542,6 +512,7 @@
{
"cell_type": "code",
"execution_count": 14,
"id": "aa29027d",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -651,6 +622,7 @@
{
"cell_type": "code",
"execution_count": 16,
"id": "8affa78c",
"metadata": {},
"outputs": [
{
Expand All @@ -667,6 +639,7 @@
},
{
"cell_type": "markdown",
"id": "5ead5f4a",
"metadata": {},
"source": [
"# Project specification"
Expand All @@ -675,6 +648,7 @@
{
"cell_type": "code",
"execution_count": 11,
"id": "d6b90116",
"metadata": {},
"outputs": [
{
Expand All @@ -701,6 +675,7 @@
},
{
"cell_type": "markdown",
"id": "13ff5249",
"metadata": {},
"source": [
"### Package name\n",
Expand All @@ -722,6 +697,7 @@
},
{
"cell_type": "markdown",
"id": "127f9e41",
"metadata": {},
"source": [
"### README\n",
Expand All @@ -731,6 +707,7 @@
},
{
"cell_type": "markdown",
"id": "05315dbb",
"metadata": {},
"source": [
"### Licensing\n",
Expand All @@ -744,6 +721,7 @@
},
{
"cell_type": "markdown",
"id": "96749323",
"metadata": {},
"source": [
"### Version\n",
Expand All @@ -758,6 +736,7 @@
},
{
"cell_type": "markdown",
"id": "5676816a",
"metadata": {},
"source": [
"### Python versioning\n",
Expand All @@ -766,6 +745,7 @@
},
{
"cell_type": "markdown",
"id": "79ce5039",
"metadata": {},
"source": [
"### Dependencies\n",
Expand All @@ -775,6 +755,7 @@
},
{
"cell_type": "markdown",
"id": "c1046053",
"metadata": {},
"source": [
"### Optional dependencies\n",
Expand All @@ -784,6 +765,7 @@
{
"cell_type": "code",
"execution_count": 16,
"id": "f46d6861",
"metadata": {},
"outputs": [
{
Expand All @@ -807,6 +789,7 @@
},
{
"cell_type": "markdown",
"id": "52bf9331",
"metadata": {},
"source": [
"We install these by calling\n",
Expand All @@ -826,6 +809,7 @@
{
"cell_type": "code",
"execution_count": 18,
"id": "a1e516d3",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -914,7 +898,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.13"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down

0 comments on commit f610f88

Please sign in to comment.