You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu16编译pycaffe遇Python.h缺失问题(python-devel无效)求解决

Fixing "fatal error: Python.h: No such file or directory" When Compiling pycaffe with Anaconda 3.6 on Ubuntu 16.04

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_PATH so the linker can find the Python libraries. Add this to your ~/.bashrc:

    export LD_LIBRARY_PATH=$HOME/anaconda3/lib:$LD_LIBRARY_PATH
    

    Then reload your bashrc with source ~/.bashrc before recompiling.

  • Verify Anaconda Headers Are Installed: If Python.h is missing in your Anaconda include directory, reinstall Python in your Anaconda environment to ensure headers are included:

    conda install python=3.6 --force-reinstall
    
  • For Python 2.7 Attempts: If you tried switching to Python 2.7, you need to update makefile.config to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:00:23