在NVIDIA PyTorch 19.05-py3容器中导入tensorrt模块失败求助
tensorrt Python module in the nvcr.io/nvidia/pytorch:19.05-py3 container (even though TensorRT C++ samples work)? Great question! Let’s break this down clearly:
Root Cause
Your TensorRT C++ samples run perfectly because the core TensorRT runtime libraries (like libnvinfer5) are installed via the Debian package you set up. However, the Python tensorrt module is missing for two key reasons:
- The
nvcr.io/nvidia/pytorch:19.05-py3image doesn’t come with TensorRT’s Python bindings pre-installed by default. - The Debian TensorRT packages you installed only include C++ libraries, headers, and samples—they don’t automatically configure the Python module to be visible to your Python interpreter.
Fixes to Try
Option 1: Install the TensorRT Python Wheel (Recommended)
Since your TensorRT version is 5.1.5.0 (matched with CUDA 10.1), grab the compatible wheel package and install it:
- Download the correct wheel for Python 3.6 (the version included in this container):
wget https://developer.download.nvidia.com/compute/machine-learning/tensorrt/secure/5.1.5.0/ubuntu1804/x86_64/TensorRT-5.1.5.0-cp36-none-linux_x86_64.whl - Install it with pip:
pip install TensorRT-5.1.5.0-cp36-none-linux_x86_64.whl - Verify the installation works:
You’ll seepython3 -c "import tensorrt; print(f'TensorRT version: {tensorrt.__version__}')"TensorRT version: 5.1.5.0if the setup was successful.
Option 2: Manually Add TensorRT’s Python Path to PYTHONPATH
If you prefer not to install the wheel, you can explicitly tell Python where to find the TensorRT module:
- Locate the TensorRT Python module directory on your system:
This will likely return a path likefind /usr -name "tensorrt" -type d/usr/lib/python3.6/dist-packages/tensorrt. - Add this path to your
PYTHONPATHenvironment variable:
To make this permanent, add the line above to yourexport PYTHONPATH=/usr/lib/python3.6/dist-packages:$PYTHONPATH~/.bashrcfile. - Test the import again:
python3 -c "import tensorrt"
Quick Clarification
The gap between working C++ samples and missing Python support is straightforward: C++ samples link directly against the pre-installed TensorRT shared libraries. Python, however, needs specific Python bindings (.so files and helper modules) to be present in its search path—something the Debian package doesn’t handle automatically for you.
内容的提问来源于stack exchange,提问作者Bilal Siddiqui

