Ubuntu16编译pycaffe遇Python.h缺失问题(python-devel无效)求解决
Hey there, I’ve run into this exact issue dozens of times—since you’re using Anaconda’s Python instead of Ubuntu’s system Python, installing python-devel (the system package) won’t help at all. Caffe is still looking for headers/libraries in the system Python paths instead of your Anaconda environment. Let’s fix this step by step:
1. Confirm Your Anaconda Python Path
First, make sure you know where your Anaconda 3.6 is installed. Run this command in your terminal:
which python
You should get something like ~/anaconda3/bin/python (adjust the path if you installed Anaconda in a different location). Verify the corresponding include directory (where Python.h lives) exists with:
ls ~/anaconda3/include/python3.6m/Python.h
2. Update makefile.config for Anaconda
Open your Caffe directory’s makefile.config (copy Makefile.config.example to Makefile.config if you haven’t already) and modify these key sections:
a. Point PYTHON_INCLUDE to Anaconda
Find the PYTHON_INCLUDE section, comment out the system Python lines, and replace them with your Anaconda paths:
# PYTHON_INCLUDE := /usr/include/python2.7 \ # /usr/lib/python2.7/dist-packages/numpy/core/include # Uncomment for Anaconda Python distribution PYTHON_INCLUDE := $(HOME)/anaconda3/include \ $(HOME)/anaconda3/include/python3.6m \ $(HOME)/anaconda3/lib/python3.6/site-packages/numpy/core/include
b. Set PYTHON_LIB to Anaconda’s Library Path
Find the PYTHON_LIB section, comment out the system line, and use your Anaconda lib path:
# PYTHON_LIB := /usr/lib PYTHON_LIB := $(HOME)/anaconda3/lib
c. Enable Python Layers
Make sure this line is uncommented (it’s required for pycaffe):
WITH_PYTHON_LAYER := 1
3. Clean Old Build Files and Recompile
Before rebuilding, clear any cached build files to avoid conflicts:
make clean
Then compile Caffe and pycaffe (use -j$(nproc) to speed up compilation with all your CPU cores):
make all -j$(nproc) make pycaffe
4. Troubleshooting If It Still Fails
Check Environment Variables: Ensure your Anaconda lib path is in
LD_LIBRARY_PATHso the linker can find the Python libraries. Add this to your~/.bashrc:export LD_LIBRARY_PATH=$HOME/anaconda3/lib:$LD_LIBRARY_PATHThen reload your bashrc with
source ~/.bashrcbefore recompiling.Verify Anaconda Headers Are Installed: If
Python.his missing in your Anaconda include directory, reinstall Python in your Anaconda environment to ensure headers are included:conda install python=3.6 --force-reinstallFor Python 2.7 Attempts: If you tried switching to Python 2.7, you need to update
makefile.configto point to your Anaconda Python 2.7 paths (e.g.,~/anaconda3/envs/py27/include/python2.7/instead of the 3.6 path) instead of just switching the Python version.
内容的提问来源于stack exchange,提问作者J. Devez

