Set proper LD_LIBRARY_PATH on Linux in nightly venv in nightly pull tool (#143262)

Before this change:

```console
$ make setup-env-cuda PYTHON="${HOMEBREW_PREFIX}/bin/python3.12"
$ source venv/bin/activate
$ python3 -c 'import torch'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/__init__.py", line 379, in <module>
    from torch._C import *  # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^
ImportError: libcudnn.so.9: cannot open shared object file: No such file or directory
```

This PR adds `site-packages/nvidia/**/lib` to `LD_LIBRARY_PATH` in `venv/bin/activate` script to let NVIDIA PyPI packages can be loaded correctly.

See also:

- #141837

Pull Request resolved: https://github.com/pytorch/pytorch/pull/143262
Approved by: https://github.com/malfet
This commit is contained in:
Xuehai Pan
2025-04-02 00:17:51 +08:00
committed by PyTorch MergeBot
parent a19b667bca
commit ae74ef9d53

View File

@ -50,6 +50,7 @@ import shutil
import subprocess
import sys
import tempfile
import textwrap
import time
import uuid
from ast import literal_eval
@ -340,6 +341,44 @@ class Venv:
self.base_python("-m", "venv", str(self.prefix))
assert self.is_venv(), "Failed to create virtual environment."
(self.prefix / ".gitignore").write_text("*\n", encoding="utf-8")
if LINUX:
activate_script = self.activate_script
st_mode = activate_script.stat().st_mode
# The activate script may be read-only and we need to add write permissions
activate_script.chmod(st_mode | 0o200)
with activate_script.open(mode="a", encoding="utf-8") as f:
f.write(
"\n"
+ textwrap.dedent(
f"""
# Add NVIDIA PyPI packages to LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$(
{self.executable.name} - <<EOS
import glob
import itertools
import os
import site
nvidia_libs = [
p.rstrip("/")
for p in itertools.chain.from_iterable(
glob.iglob(f"{{site_dir}}/{{pattern}}/", recursive=True)
for site_dir in site.getsitepackages()
for pattern in ("nvidia/**/lib", "cu*/**/lib")
)
]
ld_library_path = os.getenv("LD_LIBRARY_PATH", "").split(os.pathsep)
print(os.pathsep.join(dict.fromkeys(nvidia_libs + ld_library_path)))
EOS
)"
"""
).strip()
+ "\n"
)
# Change the file mode back
activate_script.chmod(st_mode)
return self.ensure()
def ensure(self) -> Path: