ModuleNotFoundError
ModuleNotFoundError: No module named 'bitsandbytes'
Stack trace
Traceback (most recent call last):
File "train_qlora.py", line 10, in <module>
import bitsandbytes as bnb
ModuleNotFoundError: No module named 'bitsandbytes' Why it happens
QLoRA relies on the bitsandbytes library for 4-bit quantization and efficient GPU memory usage. If bitsandbytes is not installed or incompatible with your environment, Python raises a ModuleNotFoundError when importing it.
Detection
Check for ModuleNotFoundError exceptions referencing 'bitsandbytes' during your QLoRA training script startup or import phase.
Causes & fixes
bitsandbytes library is not installed in the current Python environment
Install bitsandbytes via pip using 'pip install bitsandbytes' or 'pip install bitsandbytes-cudaXXX' matching your CUDA version.
bitsandbytes installed but incompatible CUDA version or missing GPU drivers
Ensure bitsandbytes version matches your CUDA toolkit version and GPU drivers are up to date; reinstall bitsandbytes with the correct CUDA wheel.
Running in an environment without GPU support or unsupported OS
Use CPU-only fallback or switch to a supported GPU environment; bitsandbytes requires compatible GPU hardware and drivers.
Code: broken vs fixed
import os
import bitsandbytes as bnb # ModuleNotFoundError here if bitsandbytes not installed
# QLoRA training code continues... import os
import bitsandbytes as bnb # Fixed by installing bitsandbytes via pip
# QLoRA training code continues... Workaround
If you cannot install bitsandbytes now, disable QLoRA or switch to a non-quantized training mode that does not require bitsandbytes.
Prevention
Add bitsandbytes to your project's requirements.txt or environment setup scripts and verify CUDA compatibility before deployment to avoid missing dependency errors.