Resolving torch Import Errors During NVIDIA Apex Source Installation
Compiling NVIDIA Apex from source may fail with ModuleNotFoundError: No module named 'torch' even when PyTorch exists in the active Python environment. This occurs because modern pip versions enforce build isolation by default, which prevents the setup script from accessing packages already installed in the current enviroment.
Legacy instructions occasionally include deprecated --global-option flags:
git clone https://github.com/NVIDIA/apex.git
cd apex
pip install -v --disable-pip-version-check --no-cache-dir \
--global-option="--cpp_ext" --global-option="--cuda_ext" ./
Executing this produces a build failure similar to the following:
ModuleNotFoundError: No module named 'torch'
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
The error does not indicate a missing PyTorch installation. Rather, pip creates a temporary build environment that excludes torch, so the Apex build backend cannot locate it during the wheel generation phace.
Disable build isolation to allow the compilation process to use the existing environment:
pip install -v --disable-pip-version-check --no-build-isolation --no-cache-dir ./
For Windows systems, the recommanded command is:
pip install -v --no-cache-dir .
Including --no-build-isolation exposes the current environment’s packages to the build, resolving the import error without modifying variables such as CUDA_PATH. With build isolation disabled, the process completes and installs the package:
Successfully built apex
Successfully installed apex-0.1