mirror of
https://github.com/astral-sh/uv.git
synced 2025-08-04 10:58:28 +00:00
parent
b2a21468ee
commit
dac696e950
21 changed files with 497 additions and 155 deletions
|
@ -33,3 +33,7 @@ export UV_KEYRING_PROVIDER=subprocess
|
|||
# Configure the index URL with the username
|
||||
export UV_EXTRA_INDEX_URL=https://VssSessionToken@pkgs.dev.azure.com/{organisation}/{project}/_packaging/{feedName}/pypi/simple/
|
||||
```
|
||||
|
||||
## Other indexes
|
||||
|
||||
uv is also known to work with JFrog's Artifactory, the Google Cloud Artifact Registry, and AWS Code Artifact.
|
||||
|
|
|
@ -12,14 +12,14 @@ docker run ghcr.io/astral-sh/uv --help
|
|||
|
||||
uv can be installed by copying from the official Docker image:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
FROM python:3.12-slim-bullseye
|
||||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
|
||||
```
|
||||
|
||||
Or with the standalone installer:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
FROM python:3.12-slim-bullseye
|
||||
RUN apt-get update && apt-get install -y curl --no-install-recommends
|
||||
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||
|
@ -36,19 +36,19 @@ Once uv is installed in an image, it can be used to install some packages.
|
|||
|
||||
The system Python environment is safe to use this context, since a container is already isolated. The `--system` flag can be used to install in the system environment:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
RUN uv pip install --system ruff
|
||||
```
|
||||
|
||||
To use the system Python environment by default, set the `UV_SYSTEM_PYTHON` variable:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
ENV UV_SYSTEM_PYTHON=1
|
||||
```
|
||||
|
||||
Alternatively, a virtual environment can be created and activated:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
RUN uv venv /opt/venv
|
||||
# Use the virtual environment automatically
|
||||
ENV VIRTUAL_ENV=/opt/venv
|
||||
|
@ -58,7 +58,7 @@ ENV PATH="/opt/venv/bin:$PATH"
|
|||
|
||||
When using a virtual environment, the `--system` flag should be omitted from uv invocations:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
RUN uv pip install ruff
|
||||
```
|
||||
|
||||
|
@ -66,7 +66,7 @@ RUN uv pip install ruff
|
|||
|
||||
To install requirements files, copy them into the container:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
COPY requirements.txt .
|
||||
RUN uv pip install -r requirements.txt
|
||||
```
|
||||
|
@ -75,7 +75,7 @@ RUN uv pip install -r requirements.txt
|
|||
|
||||
When installing a project alongside requirements, it is prudent to separate copying the requirements from the rest of the source code. This allows the dependencies of the project (which do not change often) to be cached separately from the project itself (which changes very frequently).
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
COPY pyproject.toml .
|
||||
RUN uv pip install -r pyproject.toml
|
||||
COPY . .
|
||||
|
@ -88,7 +88,7 @@ RUN uv pip install -e .
|
|||
|
||||
If uv isn't needed in the final image, the binary can be mounted in each invocation:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
RUN --mount=from=uv,source=/uv,target=/bin/uv \
|
||||
uv pip install --system ruff
|
||||
```
|
||||
|
@ -97,7 +97,7 @@ RUN --mount=from=uv,source=/uv,target=/bin/uv \
|
|||
|
||||
A [cache mount](https://docs.docker.com/build/guide/mounts/#add-a-cache-mount) can be used to improve performance across builds:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
RUN --mount=type=cache,target=/root/.cache/uv \
|
||||
./uv pip install -r requirements.txt -->
|
||||
```
|
||||
|
@ -105,7 +105,7 @@ RUN --mount=type=cache,target=/root/.cache/uv \
|
|||
Note the cache directory's location can be determined with the `uv cache dir` command.
|
||||
Alternatively, the cache can be set to a constant location:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile title="Dockerfile"
|
||||
ENV UV_CACHE_DIR=/opt/uv-cache/
|
||||
```
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ uv installation differs depending on the platform.
|
|||
|
||||
### on Unix
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
name: Example on Unix
|
||||
|
||||
jobs:
|
||||
|
@ -24,7 +24,7 @@ jobs:
|
|||
|
||||
### on Windows
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
name: Example on Windows
|
||||
|
||||
jobs:
|
||||
|
@ -43,7 +43,7 @@ jobs:
|
|||
|
||||
### Using a matrix
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
name: Example
|
||||
|
||||
jobs:
|
||||
|
@ -78,7 +78,7 @@ jobs:
|
|||
|
||||
Python can be installed with the `python install` command:
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
# ... setup up uv ...
|
||||
|
||||
|
@ -90,7 +90,7 @@ This will respect the Python version pinned in the project.
|
|||
|
||||
Or, when using a matrix, as in:
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
strategy:
|
||||
matrix:
|
||||
python-version:
|
||||
|
@ -101,7 +101,7 @@ strategy:
|
|||
|
||||
Provide the version to the `python install` invocation:
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
# ... setup up uv ...
|
||||
|
||||
|
@ -111,7 +111,7 @@ steps:
|
|||
|
||||
Alternatively, the official GitHub `setup-python` action can be used. This is generally faster, but will not respect the project's pinned Python version.
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
- name: "Set up Python"
|
||||
uses: actions/setup-python@v5
|
||||
|
@ -123,7 +123,7 @@ steps:
|
|||
|
||||
Once uv and Python are installed, the project can be installed with `uv sync` and commands can be run in the environment with `uv run`:
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
# ... setup up Python and uv ...
|
||||
|
||||
|
@ -139,7 +139,7 @@ steps:
|
|||
|
||||
If using the `uv pip` interface instead of the uv project interface, uv requires a virtual environment by default. To allow installing packages into the system environment, use the `--system` flag on all `uv` invocations or set the `UV_SYSTEM_PYTHON` variable, e.g.:
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
- name: Allow uv to use the system Python by default
|
||||
run: echo "UV_SYSTEM_PYTHON=1" >> $GITHUB_ENV
|
||||
|
@ -147,7 +147,7 @@ steps:
|
|||
|
||||
Now, `uv pip` can modify the system environment without creating and activating a virtual environment.
|
||||
|
||||
```yaml
|
||||
```yaml title="example.yml"
|
||||
steps:
|
||||
# ... setup up Python and uv ...
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ An official pre-commit hook is provided at [`astral-sh/uv-pre-commit`](https://g
|
|||
|
||||
To compile requirements via pre-commit, add the following to the `.pre-commit-config.yaml`:
|
||||
|
||||
```yaml
|
||||
```yaml title=".pre-commit-config.yaml"
|
||||
- repo: https://github.com/astral-sh/uv-pre-commit
|
||||
# uv version.
|
||||
rev: 0.2.27
|
||||
|
@ -16,7 +16,7 @@ To compile requirements via pre-commit, add the following to the `.pre-commit-co
|
|||
|
||||
To compile alternative files, modify `args` and `files`:
|
||||
|
||||
```yaml
|
||||
```yaml title=".pre-commit-config.yaml"
|
||||
- repo: https://github.com/astral-sh/uv-pre-commit
|
||||
# uv version.
|
||||
rev: 0.2.27
|
||||
|
@ -29,7 +29,7 @@ To compile alternative files, modify `args` and `files`:
|
|||
|
||||
To run the hook over multiple files at the same time:
|
||||
|
||||
```yaml
|
||||
```yaml title=".pre-commit-config.yaml"
|
||||
- repo: https://github.com/astral-sh/uv-pre-commit
|
||||
# uv version.
|
||||
rev: 0.2.27
|
||||
|
|
|
@ -21,7 +21,7 @@ $ uv init
|
|||
|
||||
This will create the following directory structure:
|
||||
|
||||
```
|
||||
```text
|
||||
.
|
||||
├── pyproject.toml
|
||||
├── README.md
|
||||
|
@ -52,7 +52,7 @@ run a project command.
|
|||
|
||||
The `pyproject.toml` contains metadata about your project:
|
||||
|
||||
```toml
|
||||
```toml title="pyproject.toml"
|
||||
[project]
|
||||
name = "hello-world"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -7,7 +7,7 @@ script dependencies are properly managed inside and outside of projects.
|
|||
|
||||
If your script has no dependencies, you can execute it with `uv run`:
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
print("Hello world")
|
||||
```
|
||||
|
||||
|
@ -33,7 +33,7 @@ $ uv run example.py
|
|||
|
||||
Arguments may be provided to the script:
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
import sys
|
||||
|
||||
print(" ".join(sys.argv[1:]))
|
||||
|
@ -63,7 +63,7 @@ of dependencies that are required for the script. Generally, it's recommended to
|
|||
|
||||
For example, the following script requires `rich`.
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
import time
|
||||
from rich.progress import track
|
||||
|
||||
|
@ -104,7 +104,7 @@ Python recently added a standard format for [inline script metadata](https://pac
|
|||
|
||||
To use inline script metadata, include a `script` section at the top of the script:
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
# /// script
|
||||
# dependencies = [
|
||||
# "requests<3",
|
||||
|
@ -140,7 +140,7 @@ $ uv run example.py
|
|||
|
||||
uv also supports Python version requirements:
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
# /// script
|
||||
# requires-python = ">=3.12"
|
||||
# dependencies = []
|
||||
|
@ -159,7 +159,7 @@ Note that when using inline script metadata, even if `uv run` is used in a _proj
|
|||
|
||||
uv allows arbitrary Python versions to be requested on each script invocation, for example:
|
||||
|
||||
```python
|
||||
```python title="example.py"
|
||||
import sys
|
||||
|
||||
print(".".join(map(str, sys.version_info[:3])))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue