Ubuntu16.04+Python3.5安装GPU版XGBoost遇兼容及运行错误
Hey there, I’ve dealt with similar headaches installing GPU-enabled XGBoost on older Ubuntu/Python setups before—let’s walk through fixing each of your issues step by step.
The SyntaxError from rabit’s Python files happens because older versions of the rabit submodule (or even some recent XGBoost branches) still have Python 2-style print statements (without parentheses) that break Python 3. Here’s how to fix it:
- Switch to a stable, Python 3.5-compatible branch: The default
mainbranch might have code that’s too new or not fully backported for Python 3.5. Try checking out a known compatible version like v1.0.2, which supports GPU and plays nice with Python 3.5:cd xgboost git checkout v1.0.2 git submodule update --init --recursive # Make sure rabit gets updated to the compatible version - Recompile and reinstall cleanly: First wipe old build artifacts, then redo the GPU build and Python installation:
make clean rm -rf build mkdir -p build && cd build cmake .. -DUSE_CUDA=ON make -j$(nproc) cd ../python-package python3 setup.py install --user # Using --user avoids sudo conflicts with system Python - Alternative manual fix (if switching branches isn’t an option): Find all
.pyfiles underxgboost/rabit/python/and replace every instance ofprint "some text"withprint("some text"). This is tedious but works if you need a specific branch.
Let’s tackle each of your test issues one by one:
a. Missing dtest.dm file
This is almost always a path or missing test data issue:
- Run benchmarks from the root XGBoost directory, not the
python-packagefolder. The test scripts expect to find data files in the root’s data directories. - If you haven’t already, download the official test data using the provided script:
cd xgboost ./dmlc-core/scripts/get_data.sh
b. DMatrix has no handle attribute / nthread parameter error
These issues stem from a mismatch between the compiled GPU backend and the Python API (either the package didn’t install correctly, or version mismatches):
- Uninstall and reinstall cleanly: First remove any existing XGBoost installations:
Then repeat the GPU build and installation steps above (making sure you’re in the correct branch).pip3 uninstall xgboost -y - Check parameter compatibility: For older XGBoost versions (like v1.0.2), the correct parameter for thread count is
nthread, notn_jobs(which is used in newer versions). If your benchmark script usesn_jobs, switch it tonthreadto match your installed version. - Verify GPU support: After installation, run this in a Python 3 shell to confirm everything’s working:
You should seeimport xgboost as xgb print(f"XGBoost version: {xgb.__version__}") print(f"CUDA enabled: {xgb.config.get_config()['USE_CUDA']}")USE_CUDA: 1if GPU support is correctly installed.
- Use a virtual environment to isolate your XGBoost setup from system Python dependencies. For Python 3.5, you can set one up with:
Then run all your build and install commands inside this environment.virtualenv -p python3.5 xgboost_env source xgboost_env/bin/activate - Double-check your CUDA version: XGBoost v1.0.2 requires CUDA 9.0 or newer. Ubuntu 16.04 works well with CUDA 9.1 or 10.0—make sure your installed CUDA version matches what XGBoost expects.
内容的提问来源于stack exchange,提问作者Jack Power

