From 062f6195e8768765e8aafb115c723b698787c42e Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 11:20:12 +0200 Subject: [PATCH 01/16] Add uv to dependencies We use uv instead of flit, since it combines package manager and developmenr environtment --- software/environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/software/environment.yml b/software/environment.yml index 2fd799e0..fd7e0eb5 100644 --- a/software/environment.yml +++ b/software/environment.yml @@ -17,6 +17,7 @@ dependencies: - twine - poetry - flit + - uv - scikit-learn - scalene - ruff From 2276e5d86b35177fa6308da8f98b26c85d864e2a Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 11:24:09 +0200 Subject: [PATCH 02/16] one replacement for UCV before pulling existing PR --- content/packaging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/packaging.rst b/content/packaging.rst index 46aa079c..b6e8381f 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -88,7 +88,7 @@ Now our folder should look something like this: After this we need to create a file called `pyproject.toml `__, which describes our package. -To make this easier we'll use ``flit`` (which is already installed in the +To make this easier we'll use ``uv`` (which is already installed in the course environment) in a terminal to initialize it: .. code-block:: console From 400f2e5deb356056ce71301f7778390be6adc530 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 12:40:01 +0200 Subject: [PATCH 03/16] add lice2 for licenses --- software/environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/software/environment.yml b/software/environment.yml index fd7e0eb5..e017e986 100644 --- a/software/environment.yml +++ b/software/environment.yml @@ -18,6 +18,7 @@ dependencies: - poetry - flit - uv + - lice2 - scikit-learn - scalene - ruff From c180af5160db942069a2bfa906d2c2338197ba5c Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 13:27:46 +0200 Subject: [PATCH 04/16] Update packaging to use uv --- .../packaging-example-project/pyproject.toml | 24 ++-- .../pyproject.toml-partial | 20 +-- content/packaging.rst | 120 +++++++++--------- 3 files changed, 81 insertions(+), 83 deletions(-) diff --git a/content/packaging-example-project/pyproject.toml b/content/packaging-example-project/pyproject.toml index 11b3f226..8dc28eb8 100644 --- a/content/packaging-example-project/pyproject.toml +++ b/content/packaging-example-project/pyproject.toml @@ -1,20 +1,22 @@ -[build-system] -requires = ["flit_core >=3.11,<4"] -build-backend = "flit_core.buildapi" - [project] name = "calculator_myname" -authors = [{name = "Firstname Lastname", email = "firstname.lastname@example.org"}] +version = "0.1.0" +description = "Great calculator with amazing mathematics functions" readme = "README.md" +authors = [ + {name = "Firstname Lastname", email = "firstname.lastname@example.org"} +] +requires-python = ">=3.12" license = "MIT" license-files = ["LICENSE"] -dynamic = ["version", "description"] dependencies = [ - "scipy" + "scipy>=1.18.1", ] -[project.urls] -Home = "http://www.example.org" +[build-system] +requires = ["uv_build>=0.13.9,<0.14.0"] +build-backend = "uv_build" + +[tool.uv.build-backend] +module-name = "calculator" -[tool.flit.module] -name = "calculator" \ No newline at end of file diff --git a/content/packaging-example-project/pyproject.toml-partial b/content/packaging-example-project/pyproject.toml-partial index 76281585..63777e35 100644 --- a/content/packaging-example-project/pyproject.toml-partial +++ b/content/packaging-example-project/pyproject.toml-partial @@ -1,14 +1,14 @@ -[build-system] -requires = ["flit_core >=3.11,<4"] -build-backend = "flit_core.buildapi" - [project] name = "calculator_myname" -authors = [{name = "Firstname Lastname", email = "firstname.lastname@example.org"}] +version = "0.1.0" +description = "Add your description here" readme = "README.md" -license = "MIT" -license-files = ["LICENSE"] -dynamic = ["version", "description"] +authors = [ + {name = "Firstname Lastname", email = "firstname.lastname@example.org"} +] +requires-python = ">=3.12" -[project.urls] -Home = "http://www.example.org" + +[build-system] +requires = ["uv_build>=0.13.9,<0.14.0"] +build-backend = "uv_build" diff --git a/content/packaging.rst b/content/packaging.rst index 3db4f237..ea74d8e8 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -40,7 +40,10 @@ consisting of 3 functions, each in its own file. We can then imagine that each file would contain many more functions. To make it more interesting, one of these functions will depend on an external library: ``scipy``. -These are the 3 files: + +We will start by creating a *package*, a folder with multiple Python *modules*. +We will call it ``calculator_myname`` and include the following 3 files in it: + .. literalinclude:: packaging-example-project/calculator/adding.py :caption: adding.py @@ -55,6 +58,19 @@ These are the 3 files: :language: python +This is still only a folder with multiple Python files. To make it a package, we +need to add a fourth file: + + +.. literalinclude:: packaging-example-project/calculator/__init__.py + :caption: __init__.py + :language: python + + +This ``__init__.py`` file will be the interface of our package/library. +It also holds the package docstring and the version string. +Note how it imports functions from the various modules using *relative imports* +(with the dot). @@ -69,11 +85,6 @@ Initializing the package tools [1]_, which can be used interchangeably [2]_. We will soon see that the resulting package is `pip`-installable without using uv_. - To install uv, the preferred method is to use the - `standalone installer provided here `__. - Alternatively you can obtain ``uv`` via ``pip install uv`` or - ``conda install -c conda-forge uv`` [3]_. - .. _uv: https://docs.astral.sh/uv/ .. _pdm: https://pdm-project.org/en/latest/ @@ -81,26 +92,35 @@ Initializing the package .. _flit: https://flit.pypa.io/en/stable/ -Create a new folder with the name ``calculator-myname`` (replace with *myname* with your name), +Create a new folder with the name ``calculator_myname`` (replace with *myname* with your name), open terminal and run: .. code-block:: console - $ uv init --lib - $ uv init --lib --python 3.10 --no-pin-python + $ cd calculator_myname + $ uv init + $ mv *.py src/calculator/ This result in the following set of files. -We will add a fourth file: +.. code-block:: none -.. literalinclude:: packaging-example-project/calculator/__init__.py - :caption: __init__.py - :language: python + calculator_myname + ├── src + │ ├── calculator_myname + │ ├── adding.py + │ ├── __init__.py + │ ├── integrating.py + │ ├── py.typed + │ └── subtracting.py + └── pyproject.toml + + +.. note:: + + There is one extra file here, `py.typed`. It is useful if you include + `type hints `__ with your code. Otherwise it does not do anything. -This ``__init__.py`` file will be the interface of our package/library. -It also holds the package docstring and the version string. -Note how it imports functions from the various modules using *relative imports* -(with the dot). After that let's create a file called ``README.md`` to the project root that will describe our project to other people who might want to use it. @@ -110,50 +130,21 @@ that will describe our project to other people who might want to use it. :caption: README.md :language: markdown -Now our folder should look something like this: -.. code-block:: none +We recommend adding a license file early in package development and we recommend using a commonly chosen license. For more information see +`CodeRefinery on licenses `__ - calculator_myname - ├── calculator - │ ├── adding.py - │ ├── __init__.py - │ ├── integrating.py - │ └── subtracting.py - └── README.md -After this we need to create a file called -`pyproject.toml `__, -which describes our package. -To make this easier we'll use ``uv`` (which is already installed in the -course environment) in a terminal to initialize it: +Here we inlude the MIT license using the `lice` tool .. code-block:: console + + $ lice mit > LICENSE - $ flit init - Module name [calculator]: calculator_myname - Author: Firstname Lastname - Author email: firstname.lastname@example.org - Home page: http://www.example.org - Choose a license (see http://choosealicense.com/ for more info) - 1. MIT - simple and permissive - 2. Apache - explicitly grants patent rights - 3. GPL - ensures that code based on this is shared with the same terms - 4. Skip - choose a license later - Enter 1-4: 1 - - Written pyproject.toml; edit that file to add optional extra info. - -``flit`` will ask us questions about your project and it create a -``pyproject.toml`` into the project folder. The name of the package -(Module name) should be something that is not already in use. In best -case scenario it should be the same as the Python module name. In our -case, let's use a different name and let's fix this later. -This is how we will arrange the files in the project folder/repository: +Our Python package now has the following files: .. code-block:: none - :emphasize-lines: 3-6 calculator_myname ├── calculator @@ -164,14 +155,14 @@ This is how we will arrange the files in the project folder/repository: ├── LICENSE └── README.md -Now we are ready to test the package. For this we need to be in the "root" -folder, what we have called the *project-folder*. We also need to have + + +Now we are ready to test the package. For this we need to be in the "src" +folder, where the ``calculator``. We also need to have ``scipy`` available in our environment: -.. literalinclude:: packaging-example-project/test.py -The package is not yet pip-installable, though. We will make this possible in -the next section. +.. literalinclude:: packaging-example-project/test.py Testing a local pip install @@ -185,18 +176,23 @@ what it should install. It currently looks like this: :language: toml -Let's do couple of finishing touches to it. Because we have different names -for the package and our module import, we'll add a section that specifies -that. +Let's do couple of finishing touches to it. First, we will add the scipy +dependency. + +.. code-block:: console + + $ uv add scipy -We also need to add the dependency to ``scipy``. +We should edit the description and authors fields in the first section. +In addition, because we have different names for the package and our +module import, we'll add a section that specifies that. After the changes our ``pyproject.toml`` looks like this: .. literalinclude:: packaging-example-project/pyproject.toml :caption: pyproject.toml - :emphasize-lines: 12-14,19-20 + :emphasize-lines: 12-14,20-21 :language: toml Note how our package requires ``scipy`` and we decided to not pin the version From 146fc6fbaa21ea7ee313151c738e77d79038afe0 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 14:33:33 +0200 Subject: [PATCH 05/16] Fix package name and file structure --- content/packaging.rst | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index ea74d8e8..9303d701 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -99,7 +99,7 @@ open terminal and run: $ cd calculator_myname $ uv init - $ mv *.py src/calculator/ + $ mv *.py src/calculator_myname/ This result in the following set of files. @@ -147,11 +147,13 @@ Our Python package now has the following files: .. code-block:: none calculator_myname - ├── calculator - │ ├── adding.py - │ ├── __init__.py - │ ├── integrating.py - │ └── subtracting.py + ├── src + │ ├── calculator_myname + │ ├── adding.py + │ ├── __init__.py + │ ├── integrating.py + │ ├── py.typed + │ └── subtracting.py ├── LICENSE └── README.md From 8c0da3e703d440be24fe8ca579c6db62e614ed7e Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 14:35:42 +0200 Subject: [PATCH 06/16] Update year --- content/packaging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/packaging.rst b/content/packaging.rst index 9303d701..8dfd302b 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -547,7 +547,7 @@ for publishing your packages. - PyPI is a place for Python packages - conda is similar but is not limited to Python -.. [1] In 2025, tools such as pdm_, hatch_, flit_ have similar functionality as uv_. +.. [1] In 2026, tools such as pdm_, hatch_, flit_ have similar functionality as uv_. .. [2] Some package build tools are simple and some have advanced features. Refer to `pyOpenSci's guide on the choosing the right tool `__ .. [3] `uv` is also included in the conda environment you have created in :doc:`software installation ` episode. \ No newline at end of file From d1a2ffc03f0ef703bfcfffd930fe56c958d20296 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 15:00:59 +0200 Subject: [PATCH 07/16] use uv for exercise environment --- content/packaging.rst | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index 8dfd302b..70e33dc7 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -208,13 +208,26 @@ test before trying to upload a package to PyPI or test-PyPI Sometime you need to rely on unreleased, development versions as dependencies and this is also possible. For example, to use the - latest ``xarray`` you could add:: - - dependencies = [ - "scipy", - "xarray @ https://github.com/pydata/xarray/archive/main.zip" - ] + latest ``xarray`` you could execute: + + .. code-block:: console + + $ uv add 'xarray @ https://github.com/pydata/xarray/archive/main.zip' + + + which cause ``uv`` to edit ``pyproject.toml`` as follows: + + .. code-block:: toml + + dependencies = [ + "scipy", + "xarray", + ] + + [tool.uv.sources] + xarray = { url = "https://github.com/pydata/xarray/archive/main.zip" } + .. seealso:: - `pip requirement specifiers `__ - pyOpenSci tutorial on @@ -232,7 +245,7 @@ Exercise 1 - Create a new folder outside of our example project - Create a new virtual environment and activate it (more on this in :ref:`dependency_management`) - .. hint:: To create and activate a virtual environment + .. hint:: To create and use a virtual environment :class: dropdown .. tabs:: @@ -241,22 +254,20 @@ Exercise 1 .. code-block:: bash - python -m venv .venv - source .venv/bin/activate - which python + uv init + uv run which python .. tab:: Windows .. code-block:: bat - python -m venv .venv - .venv\Scripts\activate - where python + uv init + uv run which python - Install the example package from the project folder into the new environment:: - pip install --editable /path/to/project-folder/ + uv add --editable /path/to/project-folder/ - Test the local installation: From d6740daf5d9fb74cd0979da49f19c3a99818e2f2 Mon Sep 17 00:00:00 2001 From: Jarno Rantaharju Date: Mon, 23 Nov 2026 15:02:06 +0200 Subject: [PATCH 08/16] use uv to build package Co-authored-by: Ashwin V. Mohanan <9155111+ashwinvis@users.noreply.github.com> --- content/packaging.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/content/packaging.rst b/content/packaging.rst index 70e33dc7..4e131b53 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -309,6 +309,9 @@ We need two more things: Let's try it out. First we create the distribution package:: $ python3 -m build +Let's try it out. First we create the distribution package:: + + $ uv build We need also have ``twine`` installed, but it is included in the course environment. From 6394ce836486e4c20df8255c1da76901ea40dc4a Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 15:08:03 +0200 Subject: [PATCH 09/16] use uv instead of twine --- content/packaging.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index 70e33dc7..767d0325 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -298,8 +298,7 @@ happens. We need two more things: -- We will do this using `Twine `__ so you need - to pip install that, too. + - You need an account on test-PyPI_ .. _test-PyPI: https://test.pypi.org/ @@ -308,17 +307,19 @@ We need two more things: Let's try it out. First we create the distribution package:: - $ python3 -m build + $ uv build + -We need also have ``twine`` installed, but it is included in the course -environment. And use twine to upload the distribution files to test-PyPI:: - $ twine upload -r testpypi dist/* + $ uv publish --publish-url https://test.pypi.org/legacy/ + + Publishing 2 files to https://test.pypi.org/legacy/ +Hashing calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) +Uploading calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) +calculator-myname-0.1.0-py3-none-any.whl ------------------------------ 0 B/3.25 KiB Enter username ('__token__' if using a token): - Uploading distributions to https://test.pypi.org/legacy/ - Enter your API token: .. _Create API token: https://test.pypi.org/manage/account/token/ From 853cd71221fe139c4ccca17c6801726837205988 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 15:20:30 +0200 Subject: [PATCH 10/16] Use the init file uv created --- .../calculator/__init__.py | 4 ++- content/packaging.rst | 33 +++++++++---------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/content/packaging-example-project/calculator/__init__.py b/content/packaging-example-project/calculator/__init__.py index e6d72117..8bd1750c 100644 --- a/content/packaging-example-project/calculator/__init__.py +++ b/content/packaging-example-project/calculator/__init__.py @@ -6,4 +6,6 @@ from .subtracting import subtract from .integrating import integral -__version__ = "0.1.0" +def main() -> None: + print("Hello from test!") + diff --git a/content/packaging.rst b/content/packaging.rst index 449e7c57..3d7495bb 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -58,20 +58,6 @@ We will call it ``calculator_myname`` and include the following 3 files in it: :language: python -This is still only a folder with multiple Python files. To make it a package, we -need to add a fourth file: - - -.. literalinclude:: packaging-example-project/calculator/__init__.py - :caption: __init__.py - :language: python - - -This ``__init__.py`` file will be the interface of our package/library. -It also holds the package docstring and the version string. -Note how it imports functions from the various modules using *relative imports* -(with the dot). - Initializing the package @@ -116,6 +102,19 @@ This result in the following set of files. └── pyproject.toml +The ``__init__.py`` file will be the interface of our package/library. +It also holds the package docstring and the version string. +Note how it imports functions from the various modules using *relative imports* +(with the dot). + +To make our math functions available after importing the package, we need to import them here. + +.. literalinclude:: packaging-example-project/calculator/__init__.py + :caption: __init__.py + :language: python + + + .. note:: There is one extra file here, `py.typed`. It is useful if you include @@ -318,9 +317,9 @@ And upload the distribution files to test-PyPI:: $ uv publish --publish-url https://test.pypi.org/legacy/ Publishing 2 files to https://test.pypi.org/legacy/ -Hashing calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) -Uploading calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) -calculator-myname-0.1.0-py3-none-any.whl ------------------------------ 0 B/3.25 KiB Enter username ('__token__' if using a token): + Hashing calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) + Uploading calculator-myname-0.1.0-py3-none-any.whl (3.2KiB) + calculator-myname-0.1.0-py3-none-any.whl ------------------------------ 0 B/3.25 KiB Enter username ('__token__' if using a token): From b9b266aed3c663fefbfe180f7697aec38d804253 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 15:24:44 +0200 Subject: [PATCH 11/16] windows which -> where --- content/packaging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/packaging.rst b/content/packaging.rst index 3d7495bb..c97cb560 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -261,7 +261,7 @@ Exercise 1 .. code-block:: bat uv init - uv run which python + uv run where python - Install the example package from the project folder into the new environment:: From 29f155a70f5dc02b9c4efbfcb4dc29bc98525356 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Mon, 23 Nov 2026 15:35:41 +0200 Subject: [PATCH 12/16] replace venv sections with uv init --bare --- content/packaging.rst | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index c97cb560..0f897e0f 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -253,14 +253,14 @@ Exercise 1 .. code-block:: bash - uv init + uv init --bare uv run which python .. tab:: Windows .. code-block:: bat - uv init + uv init --bare uv run where python - Install the example package from the project folder @@ -304,9 +304,6 @@ We need two more things: .. highlight:: console -Let's try it out. First we create the distribution package:: - - Let's try it out. First we create the distribution package:: $ uv build @@ -349,7 +346,7 @@ And upload the distribution files to test-PyPI:: #. Click on **Copy token** once a long string which starts with ``pypi-`` is generated. - Paste that token back into the terminal where ``twine upload ...`` is running and press ENTER. + Paste that token back into the terminal where ``uv publish ...`` is running and press ENTER. Once this is done, create yet another virtual environment and try to install from test-PyPI (adapt ``myname``). @@ -360,25 +357,26 @@ Once this is done, create yet another virtual environment and try to install fro .. code-block:: console :emphasize-lines: 4-7 - $ python3 -m venv venv-calculator - $ source venv-calculator/bin/activate - $ which python - $ python3 -m pip install \ - -i https://test.pypi.org/simple/ \ - --extra-index-url https://pypi.org/simple/ \ + $ mkdir test_environment_2 + $ cd test_environment_2 + $ uv init --bare + $ uv run which python + $ uv add \ + --index https://test.pypi.org/simple/ \ calculator_myname - $ deactivate .. tab:: Windows .. code-block:: console :emphasize-lines: 4 - $ python3 -m venv venv-calculator - $ venv-calculator\Scripts\activate - $ where python - $ python3 -m pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ calculator_myname - $ deactivate + $ mkdir test_environment_2 + $ cd test_environment_2 + $ uv init --bare + $ uv run where python + $ uv add \ + --index https://test.pypi.org/simple/ \ + calculator_myname If you upload packages to PyPI or test PyPI often you can create an API token and `save it in the .pypirc file `__. @@ -387,11 +385,9 @@ Tools that simplify sharing via PyPI ------------------------------------ The solution that we have used to create the example package (using -``setuptools`` and ``twine``) is not the only approach. There are many ways to +``uv``) is not the only approach. There are many ways to achieve this and we avoided going into too many details and comparisons to not -confuse too much. If you web-search this, you will also see that recently the -trend goes towards using ``pyproject.toml`` as more general -alternative to the previous ``setup.py``. +confuse too much. There are at least five tools which try to make the packaging and PyPI interaction easier: From 06364c19f4760dab22e35fce936f6eb62a45ed85 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Tue, 24 Nov 2026 12:55:00 +0200 Subject: [PATCH 13/16] Add pixi and uv to the dependencies section and add pixi to the environment --- content/dependencies.rst | 205 ++++++++++++++++++++++++++++++++++++--- software/environment.yml | 1 + 2 files changed, 193 insertions(+), 13 deletions(-) diff --git a/content/dependencies.rst b/content/dependencies.rst index 9097e571..1a60277d 100644 --- a/content/dependencies.rst +++ b/content/dependencies.rst @@ -82,7 +82,7 @@ Why are there two ecosystems? .. admonition:: Conda - - **Installation tool:** `conda `__, `mamba `__ + - **Installation tool:** `conda `__, `mamba `__, `pixi ` - **Summary:** Conda aims to be a more general package distribution tool and it tries to provide not only the Python packages, but also other libraries and tools needed by the Python packages. @@ -175,6 +175,7 @@ Conda ecosystem explained used by Anaconda. It is an open source project maintained by Anaconda Inc.. - `mamba `__ is a drop in replacement for conda with additional UI features. + - `pixi `__ is a newer, feature rich package manager that uses conda_forge and PyPI. Exercise 2 ---------- @@ -210,8 +211,9 @@ to the operating system's folder structure. These environments can be created by: -- `venv `__-module in base Python - `uv `__ +- `pixi `__ +- `venv `__-module in base Python - `conda `__ / `mamba `__. - pip package called `virtualenv `__ @@ -237,6 +239,92 @@ Creating Python environments .. tabs:: + .. group-tab:: Creating a uv environment + + Initialize a `uv` environment using + + .. code-block:: console + + $ uv init --bare + + This will create a text file called `pyproject.toml` that tracks your Python dependencies. + + Add packages using + + .. code-block:: console + + $ uv add numpy matplotlib pandas + + This also creates a virtual environment in your current folder. + + You can run commands using + + .. code-block:: console + + $ uv run ... + + For example, check e.g. installed versions of Python and ``numpy``: + + .. code-block:: console + + $ uv run python -c 'import sys; import numpy; print(f"Python version: {sys.version}\nNumPy version: {numpy.__version__}")' + Python version: 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] + NumPy version: 2.1.2 + + + .. group-tab:: Creating a pixi environment + + Initialize a `pixi` environment using + + .. code-block:: console + + $ pixi init + + This will create a text file called `pixi.toml` that tracks your dependencies. + + Add packages using + + .. code-block:: console + + $ pixi add python numpy matplotlib pandas + + This also creates a virtual environment in your current folder. + + You can run commands using + + .. code-block:: console + + $ pixi run ... + + For example, check e.g. installed versions of Python and ``numpy``: + + .. code-block:: console + + $ pixi run python -c 'import sys; import numpy; print(f"Python version: {sys.version}\nNumPy version: {numpy.__version__}")' + Python version: 3.15.2 | packaged by conda-forge | (main, Oct 26 2026, 11:39:52) [GCC 15.3.0] + NumPy version: 2.5.5 + + A `pixi` environment can also be activated using + + .. code-block:: console + + $ pixi shell + + This allows running python commands directly + + .. code-block:: console + + $ python -c 'import sys; import numpy; print(f"Python version: {sys.version}\nNumPy version: {numpy.__version__}")' + Python version: 3.15.2 | packaged by conda-forge | (main, Oct 26 2026, 11:39:52) [GCC 15.3.0] + NumPy version: 2.5.5 + + Exit the environment by exiting the shell: + + .. code-block:: console + + $ exit + + .. group-tab:: Creating conda environment from environment.yml Record channels and packages you need to a file called @@ -307,6 +395,8 @@ Creating Python environments $ conda deactivate + + .. group-tab:: Creating virtual environment from requirements.txt Record packages you need to a file called @@ -382,17 +472,18 @@ Creating Python environments commands you ran in the past. It only knows the state of your environment and what you're currently telling it to install. - These kinds of problems can be mitigated by recording dependencies in an - ``environment.yml`` or ``requirements.txt`` and using the relevant + These kinds of problems can be mitigated by recording dependencies in a file + and using the relevant package manager to update / recreate the environment. + Exercise 3 ---------- .. challenge:: Dependencies-3: Create a Python environment (15 min) - Use conda or venv to create the environment presented in the + Use conda, pixi or uv to create the environment presented in the example. @@ -408,6 +499,22 @@ In these cases the best practice is to add missing packages to .. tabs:: + .. group-tab:: Adding new packages to a `uv` environment + + Add packages by running + + .. code-block:: console + + $ uv add ... + + .. group-tab:: Adding new packages to a `pixi` environment + + Add packages by running + + .. code-block:: console + + $ pixi add ... + .. group-tab:: Adding new packages to a conda environment Add new packages that you want to install to @@ -461,6 +568,38 @@ compatible. .. tabs:: + .. group-tab:: `uv` with pinned versions + + When pinning versions with `uv` one can use a + variety of comparison operators: + + .. code-block:: console + + # use exactly numpy version 2.1.2 + $ uv add "numpy==2.1.2" + # numpy that is bigger or equal than version 1, but less than version 2 + $ uv add "numpy>=1,<2" + # matplotlib greater than 3.8.2 + $ uv add "matplotlib>3.8.2" + # pandas >=2.1 and <3 + $ uv add "pandas~=2.1" + + .. group-tab:: `pixi` with pinned versions + + When pinning versions with `pixi` one can use a + variety of comparison operators: + + .. code-block:: console + + # use exactly numpy version 2.1.2 + $ pixi add "numpy==2.1.2" + # numpy that is bigger or equal than version 1, but less than version 2 + $ pixi add "numpy>=1,<2" + # matplotlib greater than 3.8.2 + $ pixi add "matplotlib>3.8.2" + # pandas >=2.1 and <3 + $ pixi add "pandas~=2.1" + .. group-tab:: environment.yml with pinned versions When pinning versions in ``environment.yml`` one can use a @@ -478,7 +617,7 @@ compatible. - numpy>=1,<2 # matplotlib greater than 3.8.2 - matplotlib>3.8.2 - # pandas that is compatible with 2.1 + # pandas >=2.1 and <3 - pandas~=2.1 .. group-tab:: requirements.txt with pinned versions @@ -492,7 +631,7 @@ compatible. numpy>=1,<2 # matplotlib greater than 3.8.2 matplotlib>3.8.2 - # pandas that is compatible with 2.1 + # pandas >=2.1 and <3 pandas~=2.1 For more information on all possible specifications, see @@ -537,6 +676,25 @@ shows which packages are the important packages needed by the software. .. tabs:: + .. group-tab:: Exporting a `uv` environment + + `uv` automatically maintains a file called `uv.lock` with exact versions of all installed packages. You need both `pyproject.toml` and `uv.lock` to exactly recreate the environment. + + To recreate the environment, run + + .. code-block:: console + + $ uv sync --locked + + .. group-tab:: Exporting a `pixi` environment + + `pixi` automatically maintains a file called `pixi.lock` with exact versions of all installed packages. You need both `pixi.toml` and `pixi.lock` to exactly recreate the environment. + + .. code-block:: console + + $ pixi install --locked + + .. group-tab:: Exporting environment.yml from a conda environment Once you have activated the environment, you can run @@ -611,22 +769,22 @@ and distributing this project as Python package (see next section). Version pinning for package creators ------------------------------------ -We will talk about packaging in a different session but when you create a library and package +We talk about packaging in a different session but when you create a library and package projects, you express dependencies either in ``pyproject.toml`` (or ``setup.py``) (PyPI) or ``meta.yaml`` (conda). These dependencies will then be used by either other libraries (who in turn -write their own ``setup.py`` or ``pyproject.toml`` or ``meta.yaml``) or by +write their own ``pyproject.toml``, ``setup.py`` or ``meta.yaml``) or by people directly (filling out ``requirements.txt`` or a ``environment.yml``). Now as a library creator you have a difficult choice. You can either pin versions very -narrowly like here (example taken from ``setup.py``): +narrowly like here (example taken from ``pyproject.toml``): .. code-block:: python :emphasize-lines: 3-6 # ... - install_requires=[ + dependencies=[ 'numpy==1.19.2', 'matplotlib==3.3.2' 'pandas==1.1.2' @@ -635,13 +793,13 @@ narrowly like here (example taken from ``setup.py``): # ... or you can define a range or keep them undefined like here (example taken from -``setup.py``): +``pyproject.toml``): .. code-block:: python :emphasize-lines: 3-6 # ... - install_requires=[ + dependencies=[ 'numpy', 'matplotlib' 'pandas' @@ -700,6 +858,27 @@ Additional tips and tricks .. tabs:: + .. info:: Converting from `requirements.txt` to `pyproject.toml` + + You can convert a list of packages in requirements.txt to a `uv` project file using + + .. code-block:: console + + $ uv add -r requirements.txt + + This will install all the listed packages into a `uv` environment and maintain a list in `pyproject.toml`, but `uv` will not manage the requirements.txt file. + + + .. info:: Converting from `environment.yml to `pixi.toml` + + You can convert a conda environment to a `pixi environment with` + + .. code-block:: console + + $ pixi init --import environment.yml + + This will install all the listed packages into a `pixi` environment and maintain a list in `pixi.toml`, but `pixi` will not manage the requirements.txt file. + .. group-tab:: Creating a conda environment from requirements.txt conda supports installing an environment from ``requirements.txt``. diff --git a/software/environment.yml b/software/environment.yml index e017e986..6f52219d 100644 --- a/software/environment.yml +++ b/software/environment.yml @@ -18,6 +18,7 @@ dependencies: - poetry - flit - uv + - pixi - lice2 - scikit-learn - scalene From e0ca8eabcd2e305cce4a22da5b3c5e9eab849426 Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Tue, 24 Nov 2026 13:10:34 +0200 Subject: [PATCH 14/16] Rename the package folder --- content/packaging.rst | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index 0f897e0f..2b2d5958 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -93,7 +93,7 @@ This result in the following set of files. calculator_myname ├── src - │ ├── calculator_myname + │ ├── calculator │ ├── adding.py │ ├── __init__.py │ ├── integrating.py @@ -141,13 +141,30 @@ Here we inlude the MIT license using the `lice` tool $ lice mit > LICENSE +Finally, we need the package name to be unique and use "calculator_myname" with our own name. But we would still like to import it with ``import calculator``. To do that, we should rename the folder in `src/`. + +.. tabs:: + + .. tab:: Unix/macOS + + .. code-block:: bash + + mv src/calculator_myname src/calculator + +.. tab:: Windows + + .. code-block:: bat + + move src/calculator_myname src/calculator + + Our Python package now has the following files: .. code-block:: none calculator_myname ├── src - │ ├── calculator_myname + │ ├── calculator │ ├── adding.py │ ├── __init__.py │ ├── integrating.py From 6fbba2405af44117f7f223aed4d7ce26e6c2d26b Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Tue, 24 Nov 2026 13:17:31 +0200 Subject: [PATCH 15/16] fix incorrect tab taf --- content/dependencies.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/dependencies.rst b/content/dependencies.rst index 1a60277d..2a76c1e4 100644 --- a/content/dependencies.rst +++ b/content/dependencies.rst @@ -858,7 +858,7 @@ Additional tips and tricks .. tabs:: - .. info:: Converting from `requirements.txt` to `pyproject.toml` + .. group-tab:: Converting from `requirements.txt` to `pyproject.toml` You can convert a list of packages in requirements.txt to a `uv` project file using @@ -869,7 +869,7 @@ Additional tips and tricks This will install all the listed packages into a `uv` environment and maintain a list in `pyproject.toml`, but `uv` will not manage the requirements.txt file. - .. info:: Converting from `environment.yml to `pixi.toml` + .. group-tab:: Converting from `environment.yml to `pixi.toml` You can convert a conda environment to a `pixi environment with` From 6f5d54fa068e8e77c683bd00d1e6438158f50c1e Mon Sep 17 00:00:00 2001 From: Rantaharju Jarno Date: Tue, 24 Nov 2026 13:22:04 +0200 Subject: [PATCH 16/16] Fix indentation --- content/packaging.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/packaging.rst b/content/packaging.rst index 2b2d5958..afd170d9 100644 --- a/content/packaging.rst +++ b/content/packaging.rst @@ -151,9 +151,9 @@ Finally, we need the package name to be unique and use "calculator_myname" with mv src/calculator_myname src/calculator -.. tab:: Windows + .. tab:: Windows - .. code-block:: bat + .. code-block:: bat move src/calculator_myname src/calculator