[ UV包管理使用 ]


python uv包管理使用[https://docs.astral.sh/uv/getting-started/installation/#standalone-installer]

一. 直接使用官方推荐命令安装

curl -LsSf https://astral.sh/uv/install.sh | sh

二. 直接下载 文件

再把文件复制到这个目录下

root@iZf8zfb58x30ba21piywgxZ:/usr/local/bin# ls
uv  uvx

三. 使用

安装
uv python install 3.12
uv python install 3.11
uv python install 3.10

由于下载python很慢, 可以自己指定py版本创建虚拟环境
uv venv --python /opt/homebrew/bin/python3.8 venv-py3.8

激活
source venv-py3.8/bin/activate

Chen-Hai-F: root@iZf8zfb58x30ba21piywgxZ:~# uv python install 3.12 error: Failed to install cpython-3.12.9-linux-x86_64-gnu Caused by: Failed to download https://github.com/astral-sh/python-build-standalone/releases/download/20250317/cpython-3.12.9%2B20250317-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz Caused by: Request failed after 3 retries Caused by: error sending request for url (https://github.com/astral-sh/python-build-standalone/releases/download/20250317/cpython-3.12.9%2B20250317-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz) Caused by: operation timed out

GitHub Copilot: 看起来使用 uv 工具安装 Python 3.12 时遇到了网络超时问题。这可能是由于 GitHub 连接不稳定导致的。我建议尝试以下几种替代方案:

  1. 使用系统包管理器安装:
sudo apt update
sudo apt install python3.12
  1. 如果上面的方法不行,可以尝试添加 deadsnakes PPA 后安装:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12
  1. 或者可以重试 uv 安装,但增加重试次数和超时时间:
UV_RETRY_TIMES=5 UV_TIMEOUT=120 uv python install 3.12
  1. 如果仍然失败,可以考虑使用 pyenv 安装:
curl https://pyenv.run | bash
pyenv install 3.12.9
pyenv global 3.12.9

GitHub Copilot: 是的,如果您已经有了自己的 Python 安装版本,可以通过以下方式在 uv 中指定使用:

  1. 首先查看当前系统上的 Python 安装:
uv python list
  1. 使用 pin 命令指定特定的 Python 版本:
uv python pin /path/to/your/python

或者您也可以通过环境变量设置:

export UV_PYTHON=/path/to/your/python

如果您想查看 uv 能找到的 Python 安装:

uv python find

如果您想永久设置某个项目使用特定的 Python 版本,可以在项目根目录创建 .uv.toml 文件:

[python]
path = "/path/to/your/python"

在旧的项目中使用

1.初始化

cd cews
uv init

2.配置文件(设置python版本,换源)

[project]
name = "cews"
version = "0.1.0"
description = "A CEWS project"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "argon2-cffi==21.3.0",
    "black==23.3.0",
    "celery==5.2.7",
    "chardet==5.2.0",
    "coverage==7.2.3",
    "crispy-bootstrap5==0.7",
    "django==4.1.8",
    "django-allauth==0.54.0",
    "django-celery-beat==2.5.0",
    "django-celery-results>=2.6.0",
    "django-compressor==4.3.1",
    "django-cors-headers==3.14.0",
    "django-coverage-plugin==3.0.0",
    "django-crispy-forms==2.0",
    "django-debug-toolbar==4.0.0",
    "django-environ==0.10.0",
    "django-extensions==3.2.1",
    "django-filter>=23.5",
    "django-model-utils==4.3.1",
    "django-qr-code==3.1.1",
    "django-redis==5.2.0",
    "django-sslserver>=0.22",
    "django-stubs==4.2.0",
    "djangorestframework==3.14.0",
    "djangorestframework-stubs==3.14.0",
    "djongo==1.3.6",
    "drf-spectacular==0.26.2",
    "factory-boy==3.2.1",
    "flake8==6.0.0",
    "flake8-isort==6.0.0",
    "flower==1.2.0",
    "hiredis==2.2.2",
    "ipdb==0.13.13",
    "mypy==1.2.0",
    "office365-rest-python-client>=2.6.2",
    "openpyxl>=3.1.5",
    "pandas>=2.3.2",
    "pillow==9.5.0",
    "pre-commit==3.2.2",
    "psycopg2-binary>=2.9.10",
    "pylint-celery==0.3",
    "pylint-django==2.5.3",
    "pymongo==3.12.3",
    "pytest==7.3.1",
    "pytest-django==4.5.2",
    "pytest-sugar==0.9.7",
    "python-slugify==8.0.1",
    "pytz==2023.3",
    "qrcode>=8.2",
    "rcssmin==1.1.1",
    "redis==4.5.4",
    "reportlab==4.0.8",
    "sphinx==6.2.1",
    "sphinx-autobuild==2021.3.14",
    "watchfiles==0.19.0",
    "werkzeug[watchdog]==2.3.3",
    "whitenoise==6.4.0",
    "xlsxwriter>=3.2.9",
]

[[tool.uv.index]]
name = "aliyun"
url = "https://mirrors.aliyun.com/pypi/simple" 
  1. 读取之前的以来到这个文件
uv add -r backend/requirements/local.txt

正常安装库命令
uv add psycopg2-binary

注意: - 路径需要指向 Python 可执行文件 - 确保指定的 Python 版本具有执行权限 - 建议使用绝对路径以避免路径解析问题