代码存放路径 和 文件布局

参考:

默认行为:

  • 如果没有 src 文件夹, pdm 认为项目是 flat layout
  • 即:

    • pdm 支持 src layout
    • pdm 支持 flat layout

      • 注意,不要生成 src 文件夹

venv 管理

使用 conda

  • 创建

    1
    
    pdm venv create -w conda -n venv38 3.8
  • activate

    1
    2
    3
    4
    5
    6
    7
    
    eval `pdm venv activate`        # 验证有效
    
    # linux 上
    `pdm venv activate`             # 验证有效
    
    # pwsh 上
    iex $(pdm venv activate)
  • deactivate

    1
    
    conda deactivate

pypi pip 源

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# * 全局
# 清华
pdm config pypi.url https://pypi.tuna.tsinghua.edu.cn/simple
# 豆瓣
pdm config pypi.url https://pypi.douban.com/simple/
# tencent 腾讯
pdm config pypi.url https://mirrors.cloud.tencent.com/pypi/simple
# 华为
pdm config pypi.url https://repo.huaweicloud.com/repository/pypi/simple


# * 本地,项目内部
pdm config --local pypi.url https://pypi.tuna.tsinghua.edu.cn/simple
pdm config --local pypi.url https://mirrors.cloud.tencent.com/pypi/simple

安装 torch 方法

指定 cuda index source

参考:

验证有效方法:

1
2
3
4
[[tool.pdm.source]]
name = "PyTorch"
url = "https://download.pytorch.org/whl/cu118"
include_packages = ["torch"]
  • like: pip install torch --index-url https://download.pytorch.org/whl/cu118

安装源配置

参考:

设置优先级

1
2
3
# 确保按先后顺序优先级使用设置的source
[tool.pdm.resolution]
respect-source-order = true

like pip –index-url (-i)

要点:

  • 使用 type = "index"

举例:

  • pytorch 2.1.1 + cuda 11.8 安装:

    • pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
  • pdm 等价命令:

    • 在设置好 pyproject.toml 后
    • pdm add torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0
  • 导出到 requirements.txt 方法

    • pdm export --format requirements --without-hashes
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 确保按先后顺序优先级使用设置的source
[tool.pdm.resolution]
respect-source-order = true


[[tool.pdm.source]]
name = "private"
url = "https://download.pytorch.org/whl/cu118"
include_packages = ["torch", "torchvision", "torchaudio"]
type = "index"
# include_packages = ["foo", "foo-*"]
# exclude_packages = ["bar-*"]

导出成 requirements.txt 结果举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# This file is @generated by PDM.
# Please do not edit it manually.

anyio==4.2.0
astor==0.8.1
certifi==2023.11.17
charset-normalizer==3.3.2
decorator==5.1.1
exceptiongroup==1.2.0; python_version < "3.11"
filelock==3.13.1
fsspec==2023.12.2
h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
idna==3.6
jinja2==3.1.2
markupsafe==2.1.3
mpmath==1.3.0
networkx==3.1
numpy==1.24.4
opt-einsum==3.3.0
paddlepaddle-gpu==2.6.0.post117
pillow==10.1.0
protobuf==3.20.2
requests==2.31.0
sniffio==1.3.0
sympy==1.12
torch==2.1.0+cu118
torchaudio==2.1.0+cu118
torchvision==0.16.0+cu118
triton==2.1.0
typing-extensions==4.9.0
urllib3==2.1.0
--find-links https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
--extra-index-url https://download.pytorch.org/whl/cu118

like pip –find-links (-f)

要点:

  • 使用 type = "find_links"

举例:

  • paddlepaddle-gpu 安装:Run the following command to install
  • pdm add paddlepaddle-gpu==2.6.0.post117 等价于
  • python -m pip install paddlepaddle-gpu==2.6.0.post117 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
  • 导出到 requirements.txt 方法

    • pdm export --format requirements --without-hashes
1
2
3
4
5
6
[[tool.pdm.source]]
name = "private"
url = "https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html"
type = "find_links"

include_packages = ["paddlepaddle-gpu"]

导出结果举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# This file is @generated by PDM.
# Please do not edit it manually.

anyio==4.2.0
astor==0.8.1
certifi==2023.11.17
decorator==5.1.1
exceptiongroup==1.2.0; python_version < "3.11"
h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
idna==3.6
numpy==1.24.4
opt-einsum==3.3.0
paddlepaddle-gpu==2.6.0.post117
pillow==10.1.0
protobuf==3.20.2
sniffio==1.3.0
typing-extensions==4.9.0; python_version < "3.11"
--find-links https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html

安装 cache

参考:

类似 pnpm, 中心化安装,具体的 venv 会软链接到实际的 cache 位置

启用方法:

1
pdm config install.cache on