@@ -22,6 +22,16 @@ interfaces)`. We can also create them with ``uv init --package``:
22
22
└── myapp
23
23
└── __init__.py
24
24
25
+ .. note ::
26
+ I strongly believe that a Python application should be properly packaged to
27
+ enjoy the many benefits, such as
28
+
29
+ * source management with :doc: `importlib <python3:library/importlib >`
30
+ * executable scripts with ``project.scripts `` instead of attached
31
+ :file: `scripts ` folders
32
+ * the benefits of :file: `src ` layout with a common, documented and well
33
+ understood structure.
34
+
25
35
:file: `myapp/pyproject.toml `
26
36
The :file: `pyproject.toml ` file contains a ``scripts `` entry point
27
37
``myapp:main ``:
@@ -56,90 +66,88 @@ interfaces)`. We can also create them with ``uv init --package``:
56
66
>>> myapp.main()
57
67
Hello from myapp!
58
68
59
- .. note ::
60
- I strongly believe that a Python application should be properly packaged to
61
- enjoy the many benefits, such as
62
-
63
- * source management with :doc: `importlib <python3:library/importlib >`
64
- * executable scripts with ``project.scripts `` instead of attached
65
- :file: `scripts ` folders
66
- * the benefits of :file: `src ` layout with a common, documented and well
67
- understood structure.
68
-
69
69
.. _uv_lock :
70
70
71
71
:file: `uv.lock ` file
72
- --------------------
73
-
74
- With ``uv add --dev . `` the :file: `uv.lock ` file was also created alongside the
75
- :file: `pyproject.toml ` file. :file: `uv.lock ` is a cross-platform lock file that
76
- records the packages that are to be installed across all possible Python
77
- features such as operating system, architecture and Python version.
78
-
79
- Unlike :file: `pyproject.toml `, which specifies the general requirements of your
80
- project, :file: `uv.lock ` contains the exact resolved versions that are installed
81
- in the project environment. This file should be checked into the :doc: `Git
82
- <Python4DataScience:productive/git/index>` version control system to enable
83
- consistent and reproducible installations on different computers.
84
-
85
- .. literalinclude :: myapp/uv.lock
86
- :caption: myapp/uv.lock
87
-
88
- :file: `uv.lock ` is a human-readable
89
- :doc: `Python4DataScience:data-processing/serialisation-formats/toml/index ` file,
90
- but is managed by ``uv `` and should not be edited manually.
91
-
92
- .. note ::
93
- If ``uv `` is to be integrated into other tools or workflows, you can export
94
- the content to the `requirements file format
95
- <https://pip.pypa.io/en/stable/reference/requirements-file-format/> `_ using
96
- :samp: `uv export --format requirements-txt > { CONSTRAINTS.TXT } `. Conversely,
97
- the :samp: `{ CONSTRAINTS.TXT } ` file created can then be used with ``uv pip
98
- install `` or other tools.
72
+ With ``uv add --dev . `` the :file: `uv.lock ` file was also created alongside
73
+ the :file: `pyproject.toml ` file. :file: `uv.lock ` is a cross-platform lock
74
+ file that records the packages that are to be installed across all possible
75
+ Python features such as operating system, architecture and Python version.
76
+
77
+ Unlike :file: `pyproject.toml `, which specifies the general requirements of
78
+ your project, :file: `uv.lock ` contains the exact resolved versions that are
79
+ installed in the project environment. This file should be checked into the
80
+ :doc: `Git <Python4DataScience:productive/git/index >` version control system
81
+ to enable consistent and reproducible installations on different computers.
82
+
83
+ .. literalinclude :: myapp/uv.lock
84
+ :caption: myapp/uv.lock
85
+
86
+ :file: `uv.lock ` is a human-readable
87
+ :doc: `Python4DataScience:data-processing/serialisation-formats/toml/index `
88
+ file, but is managed by ``uv `` and should not be edited manually.
89
+
90
+ .. note ::
91
+ If ``uv `` is to be integrated into other tools or workflows, you can
92
+ export the content to the `requirements file format
93
+ <https://pip.pypa.io/en/stable/reference/requirements-file-format/> `_
94
+ using :samp: `uv export --format requirements-txt > { CONSTRAINTS.TXT } `.
95
+ Conversely, the :samp: `{ CONSTRAINTS.TXT } ` file created can then be used
96
+ with ``uv pip install `` or other tools.
97
+
98
+ .. _reproduce-virtual-env :
99
+
100
+ Reproducing the Python environment
101
+ ----------------------------------
102
+
103
+ In production environments, you should always use exactly the versions that have
104
+ been tested. You can use ``uv sync --locked `` in your environment to ensure that
105
+ the :file: `uv.lock ` file matches the project metadata. Otherwise an error
106
+ message will be displayed.
107
+
108
+ You can then use ``uv sync --frozen `` in the production environment to ensure
109
+ that the versions of :file: `uv.lock ` are used as the source of truth, but if
110
+ the :file: `uv.lock ` file is missing in the production environment, ``uv sync
111
+ --frozen `` will terminate with an error. Finally, changes to dependencies in the
112
+ :file: `pyproject.toml ` file are ignored if they are not yet written to the
113
+ :file: `uv.lock ` file.
114
+
115
+ If you want to use ``uv run `` in a productive environment, the ``--no-sync ``
116
+ option prevents the environment from being updated.
99
117
100
118
.. _update-uv-lock :
101
119
102
- Aktualisieren von :file: `uv.lock `
103
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104
-
105
- :file: `uv.lock ` is updated regularly when ``uv sync `` and ``uv run `` are called.
106
-
107
- ``--frozen ``
108
- leaves the :file: `uv.lock file ` unchanged.
109
- ``--no-sync ``
110
- avoids updating the environment during ``uv run `` calls.
111
- ``--locked ``
112
- ensures that the lock file matches the project metadata. If the lockfile is
113
- not up-to-date, an error message is issued instead of updating the lockfile.
120
+ Updating the Python environment
121
+ -------------------------------
114
122
115
123
By default, ``uv `` favours the locked versions of the packages when executing
116
124
``uv sync `` and ``uv lock ``. Package versions are only changed if the dependency
117
125
conditions of the project exclude the previous, locked version.
118
126
119
- ``uv lock --upgrade ``
120
- updates all packages.
121
- :samp: `uv lock --upgrade-package { PACKAGE } =={ VERSION } `
122
- upgrades a single package to a specific version.
127
+ With ``uv lock --upgrade `` you can upgrade all packages and with :samp: `uv lock
128
+ --upgrade-package { PACKAGE } =={ VERSION } ` you can upgrade individual packages to a
129
+ specific version.
123
130
124
- You can also use the
125
- :doc: `Python4DataScience:productive/git/advanced/hooks/pre-commit ` to regularly
126
- update your uv.lock file:
131
+ .. tip ::
132
+ You can also use the
133
+ :doc: `Python4DataScience:productive/git/advanced/hooks/pre-commit ` to
134
+ regularly update your :file: `uv.lock ` file:
127
135
128
- .. code-block :: yaml
129
- :caption : .pre-commit-config.yaml
136
+ .. code-block :: yaml
137
+ :caption : .pre-commit-config.yaml
130
138
131
- - repo : https://github.com/astral-sh/uv-pre-commit
132
- rev : 0.4.24
133
- hooks :
134
- - id : uv-lock
139
+ - repo : https://github.com/astral-sh/uv-pre-commit
140
+ rev : 0.5.21
141
+ hooks :
142
+ - id : uv-lock
135
143
136
144
Restrict platform and Python versions
137
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
+ -------------------------------------
138
146
139
147
If your project only supports a limited number of platforms or Python versions,
140
- you can do this in the :file: `pyprojects.toml ` file in compliance with
141
- :pep: ` 508 `, for example to restrict the :file: ` uv.lock ` file to macOS and Linux
142
- only :
148
+ you can do this in the :file: `pyprojects.toml ` file :pep: ` 508 ` compliant, for
149
+ example to restrict your project to macOS and Linux only you can add the
150
+ following section in your :file: ` pyproject.toml ` file :
143
151
144
152
.. code-block :: toml
145
153
0 commit comments