Open lancetipton opened 8 months ago
This is exactly what frustrates me as well 🫠 @lancetipton Could you find any workaround until this issue gets fixed?
@mpaluch92 - I did come up with a script as a workaround that handles it for my use case. It's not perfect, but it does keep the unwanted dependencies out of the pdm.lock
file.
Downside is the unwanted dependencies are still installed any time you add a new package
pdm add some-package -dG pipeline
pdm.lock
file.Upside is they are not saved to the pdm.lock
file, so they are not installed in other locations like my CI environment
pdm install
pdm.lock
fileHere's the script if you want it. You'll have to customize it a bit to your needs, but hopefully it's straight forward enough:
try:
import tomlkit
noTomlKit = False
except:
noTomlKit = True
from pathlib import Path
dirPath = Path(__file__).parent
rootDir = str((dirPath / '../').resolve())
lockLoc = (rootDir + '/pdm.lock')
config = {
'lock': {
'packageKey': 'package',
},
'remove': [
"nvidia-cublas-cu12",
"nvidia-cuda-cupti-cu12",
"nvidia-cuda-nvrtc-cu12",
"nvidia-cuda-runtime-cu12",
"nvidia-cudnn-cu12",
"nvidia-cufft-cu12",
"nvidia-curand-cu12",
"nvidia-cusolver-cu12",
"nvidia-cusparse-cu12",
"nvidia-nccl-cu12",
"nvidia-nvjitlink-cu12",
"nvidia-nvtx-cu12",
"triton",
],
'deps': [
{
'name': 'torch',
'remove': [
"nvidia-cublas-cu12",
"nvidia-cuda-cupti-cu12",
"nvidia-cuda-nvrtc-cu12",
"nvidia-cuda-runtime-cu12",
"nvidia-cudnn-cu12",
"nvidia-cufft-cu12",
"nvidia-curand-cu12",
"nvidia-cusolver-cu12",
"nvidia-cusparse-cu12",
"nvidia-nccl-cu12",
"nvidia-nvjitlink-cu12",
"nvidia-nvtx-cu12",
"triton",
]
}
]
}
def loadLockFile():
"""Loads the PDM lockfile to be updated"""
content = Path(lockLoc).read_text()
return tomlkit.loads(content)
def saveLockFile(lockFile):
"""Save the update lockfile"""
updated = tomlkit.dumps(lockFile)
Path(lockLoc).write_text(updated)
def removePackages(lockFile={}, config={}):
"""Remove top level packages from the lockfile"""
packages = lockFile.get('package')
remove = config.get('remove')
updated = []
for package in packages:
if package.get('name') not in remove:
updated.append(package)
lockFile['package'] = updated
return lockFile
def removeDeps(lockFile={}, config={}):
"""Remove child dependencies from packages in the lockfile"""
packages = lockFile.get('package')
updated = []
cdeps = config.get('deps')
for cdep in cdeps:
name = cdep.get('name')
remove = cdep.get('remove')
for package in packages:
if package.get('name') != name:
updated.append(package)
continue
cleaned = []
deps = package.get('dependencies')
for dep in deps:
isMatch = False
for item in remove:
if dep.startswith(item):
isMatch = True
break
if not isMatch: cleaned.append(dep)
package['dependencies'] = cleaned
updated.append(package)
lockFile['package'] = updated
return lockFile
def main(config={}):
if noTomlKit:
return print('[Warning] Can not fix lockfile, missing required tomlkit dependency')
lockFile = loadLockFile()
updatedLock = removePackages(lockFile=lockFile, config=config)
finalLock = removeDeps(lockFile=updatedLock, config=config)
saveLockFile(lockFile=finalLock)
main(config=config)
Then update your pyproject.toml
file to include this in the [tool.pdm.scripts]
section:
# ... rest of config file
[tool.pdm.scripts]
# ... other scripts
post_add = {cmd = "python path/to/fixLock.py"}
post_lock = {cmd = "python path/to/fixLock.py"}
post_install = {cmd = "python path/to/fixLock.py"}
# ... rest of config file
Note I added the tomlkit check as a warning because I install different pacakges based on the environment where its running In some cases (Docker), I need to run
pdm install --frozen-lockfile
, but I don't want to include unused packages in the image, in this casetomlkit
. So it just prints a warning, and exists with a0
status.
@lancetipton Awesome! Thank you very much ❤️
Make sure you run commands with
-v
flag before pasting the output.Steps to reproduce
pdm init
pyproject.toml
file to look like this[project] name = "pdm-bug" version = "0.1.0" description = "Default template for PDM package" dependencies = [] requires-python = "==3.11.*" readme = "README.md" license = {text = "MIT"}
This is the important part
[[tool.pdm.source]] type = "find_links" url = "https://download.pytorch.org/whl/cpu/torch_stable.html" name = "torch"
[tool.pdm.dev-dependencies] pipeline = [ "torch<3.0.0,>=2.2.1", ]
End of important part
[tool.pdm] distribution = false