簡體   English   中英

致命錯誤:Python.h:沒有這樣的文件或目錄

[英]fatal error: Python.h: No such file or directory

我正在嘗試使用 C 擴展文件構建共享庫,但首先我必須使用以下命令生成 output 文件:

gcc -Wall utilsmodule.c -o Utilc

執行命令后,我收到以下錯誤消息:

> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.

我已經通過互聯網嘗試了所有建議的解決方案,但問題仍然存在。 我對Python.h沒有任何問題。 我設法在我的機器上找到該文件。

看起來你沒有正確安裝 python 開發的頭文件和靜態庫。 使用您的包管理器在系統范圍內安裝它們。

對於aptUbuntu、Debian... ):

sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

對於yumCentOS,RHEL... ):

sudo yum install python-devel    # for python2.x installs
sudo yum install python3-devel   # for python3.x installs

對於dnfFedora... ):

sudo dnf install python2-devel  # for python2.x installs
sudo dnf install python3-devel  # for python3.x installs

對於zypper ( openSUSE... ):

sudo zypper in python-devel   # for python2.x installs
sudo zypper in python3-devel  # for python3.x installs

對於apk高山... ):

# This is a departure from the normal Alpine naming
# scheme, which uses py2- and py3- prefixes
sudo apk add python2-dev  # for python2.x installs
sudo apk add python3-dev  # for python3.x installs

對於apt-cyg ( Cygwin... ):

apt-cyg install python-devel   # for python2.x installs
apt-cyg install python3-devel  # for python3.x installs

注意: python3-dev 不會自動覆蓋 python3 的所有次要版本,如果您使用例如 python 3.8,您可能需要安裝 python3.8-dev。

在 Ubuntu 上,我運行的是 Python 3,我必須安裝

sudo apt-get install python3-dev

如果要使用未鏈接到 python3 的 Python 版本,請安裝關聯的 python3.x-dev 包。 例如:

sudo apt-get install python3.5-dev

特別是對於Python 3.7Ubuntu ,我需要

sudo apt install libpython3.7-dev

. 我認為在某些時候名稱已從pythonm.n-dev更改為此。

對於 Python 3.6、3.8 到 3.10(以及計數……)類似:

sudo apt install libpython3.6-dev

sudo apt install libpython3.8-dev

sudo apt install libpython3.9-dev

sudo apt install libpython3.10-dev

你必須做兩件事。

安裝 Python 的開發包,如果是 Debian/Ubuntu/Mint,它是通過命令完成的:

sudo apt-get install python-dev

第二件事是默認情況下包含文件不在包含路徑中,Python庫默認情況下也沒有與可執行文件鏈接。 您需要添加這些標志(相應地替換 Python 的版本):

-I/usr/include/python2.7 -lpython2.7 

換句話說,您的編譯命令應該是:

gcc -Wall -I/usr/include/python2.7 -lpython2.7  utilsmodule.c -o Utilc 

如果您使用的是樹莓派:

sudo apt-get install python3-dev

在 Fedora 上為 Python 2 運行這個:

sudo dnf install python2-devel

對於 Python 3:

sudo dnf install python3-devel

確保 Python 開發文件隨您的操作系統一起提供。

您不應該對庫和包含路徑進行硬編碼。 相反,請使用 pkg-config,它將為您的特定系統輸出正確的選項:

$ pkg-config --cflags --libs python2
-I/usr/include/python2.7 -lpython2.7

您可以將其添加到您的gcc行:

gcc -Wall utilsmodule.c -o Utilc $(pkg-config --cflags --libs python2) 

如果您使用tox在多個 Python 版本上運行測試,則可能需要為您正在測試的每個 Python 版本安裝 Python 開發庫。

sudo apt-get install python2.6-dev 
sudo apt-get install python2.7-dev 
etc.

Cygwin的解決方案

您需要安裝包python2-develpython3-devel ,具體取決於您使用的 Python 版本。

您可以使用Cygwin.com32 位64 位setup.exe (取決於您的安裝)快速安裝它。

示例(如果需要,修改setup.exe的文件名和 Python 的主要版本):

$ setup.exe -q --packages=python3-devel

您還可以查看我的其他答案以獲取更多從命令行安裝 Cygwin 軟件包的選項。

對我來說,將其更改為此有效:

#include <python2.7/Python.h>

我找到了文件/usr/include/python2.7/Python.h ,並且由於/usr/include已經在包含路徑中,那么python2.7/Python.h應該就足夠了。

您也可以從命令行添加包含路徑 - gcc -I/usr/lib/python2.7 (感謝@erm3nda)。

在 AWS API (centOS) 中

yum install python27-devel

AWS EC2 安裝運行 python34:

sudo yum install python34-devel

如果您使用帶有 3.6 python 的 virtualenv(現在是邊緣),請務必安裝匹配的 python 3.6 dev sudo apt-get install python3.6-dev ,否則執行sudo python3-dev將安裝 python dev 3.3.3- 1,解決不了問題。

就我而言,在 Ubuntu 中修復它的是安裝軟件包libpython-all-dev (或libpython3-all-dev ,如果您使用 Python 3)。

情況不一樣,但它也適用於我,現在我可以將SWIGPython3.5一起使用:

我試圖編譯:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

使用 Python 2.7 可以正常工作,而不是使用我的 3.5 版:

exists_wrap.c:147:21: 致命錯誤: Python.h: No existe el archivo o el Directorio 編譯終止。

在我的 Ubuntu 16.04 安裝中運行后:

sudo apt-get install python3-dev  # for python3.x installs

現在我可以毫無問題地編譯 Python3.5:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

我設法解決了這個問題並在一個命令中生成了 .so 文件

gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7  utilsmodule.c

我在ubuntu中安裝coolprop時也遇到了這個錯誤。

對於帶有 python 3.6 的 ubuntu 16.04

sudo apt-get install python3.6-dev

如果這不起作用,請嘗試安裝/更新gcc lib。

sudo apt-get install gcc

嘗試apt文件。 很難記住丟失文件所在的包名稱。 它對任何包文件都是通用且有用的。

例如:

root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root@ubuntu234:~/auto# 

現在,您可以做出專家猜測,選擇哪一個。

對於 CentOS 7:

sudo yum install python36u-devel

我按照此處的說明在多個 VM 上安裝 python3.6: https ://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming- environment-on-centos-7 ,然后能夠構建 mod_wsgi 並使其與 python3.6 virtualenv 一起工作

對於那里的 OpenSuse 同志:

sudo zypper install python3-devel

當您安裝了不同的 Python 版本並且您使用的 pip 不是系統的 pip 時,也會出現此問題。 在這種情況下,非系統 pip 將找不到正確版本的 Python 標頭。

當我嘗試為與應用程序捆綁的 Python安裝包時,它發生在我身上。 因為它不是系統的python,所以apt install pythonXX-dev沒有用。

在這種情況下,解決方案是找到正確的 python 標頭:

find / -iname 'Python.h'

在輸出中,您將看到系統 python 標頭,並且希望是您正在尋找的標頭,例如:

/usr/include/python3.7m/Python.h
/usr/include/python3.6m/Python.h
/home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.8.5-h7579374_1/include/python3.8/Python.h
/home/ubuntu/miniconda3/pkgs/python-3.7.0-h6e4f718_3/include/python3.7m/Python.h
/home/ubuntu/miniconda3/include/python3.8/Python.h
/home/ubuntu/miniconda3/envs/sim/include/python3.7m/Python.h
/home/ubuntu/src/blender-deps/Python-3.7.7/Include/Python.h
/opt/lib/python-3.7.7/include/python3.7m/Python.h

然后,您可以設置一個編譯器標志,當被 pip 調用時,該標志將被 gcc 使用。 我的是/home/ubuntu/workspace/blender-git/lib/linux_centos7_x86_64/python/include/python3.7m/Python.h,所以我做了:

export CPPFLAGS=-I/home/ubuntu/src/blender-deps/Python-3.7.7/Include
pip install <package>

如果您在 Amazon Linux 上使用 Python 3.6(基於 RHEL,但此處給出的 RHEL 答案不起作用):

sudo yum install python36-devel

這是另一個解決方案,因為這些解決方案都不適合我。 作為參考,我試圖在 Python 3.6 的 Amazon Linux AMI 基礎 Docker 映像上pip install一些東西。

非碼頭解決方案:

# Install python3-devel like everyone says
yum -y install python36-devel.x86_64

# Find the install directory of `Python.h`
rpm -ql python36-devel.x86_64 | grep -i "Python.h"

# Forcefully add it to your include path
C_INCLUDE_PATH='/usr/include/python3.6m'
export C_INCLUDE_PATH

碼頭工人解決方案:

# Install python3-devel like everyone says
RUN yum -y install python36-devel.x86_64

# Find the install directory of `Python.h`, for me it was /usr/include/python3.6m
RUN rpm -ql python36-devel.x86_64 | grep -i "Python.h" && fake_command_so_docker_fails_and_shows_us_the_output

# Since the previous command contains a purposeful error, remove it before the next run

# Forcefully add it to your include path
ARG C_INCLUDE_PATH='/usr/include/python3.6m'

注意:如果您在編譯 C++ 時遇到錯誤,請使用CPLUS_INCLUDE_PATH

或者,您可能更喜歡使用另一個 Docker 映像。 例如,我試圖在python:3.9.4-slim上安裝asyncpg~=0.24.0 ,這會產生與您看到的相同的錯誤。 但是,當我將圖像更新為python:3時,它運行良好。

  1. 如果您的操作系統沒有附帶 Python 開發文件,則必須在您的操作系統上安裝 Python 開發文件。 這個問題的許多答案顯示了在不同系統上可以實現的無數方法。

  2. 完成后,問題是告訴編譯器它們的位置以及如何針對它們進行編譯。 Python 帶有一個名為python-config的程序。 對於編譯,您需要--includes輸出和針對 Python 庫鏈接程序(將 Python 嵌入到您的程序中) --ldflags輸出。 例子:

     gcc -c mypythonprogram.c $(python3-config --includes) gcc -o program mypythonprogram.o $(python3-config --ldflags)

python-config程序可以以 Python 版本命名 - 例如在 Debian、Ubuntu 上,這些可以命名為python3-configpython3.6-config

當然python-devlibpython-all-dev是( aptinstall的第一件事,但如果這對我的情況沒有幫助,我建議你通過sudo apt-get install libffi-dev安裝外部函數接口sudo apt-get install libffi-devsudo pip install cffi

這應該會有所幫助,特別是如果您看到/來自c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory的錯誤。

嘗試找到您的 Python.h:

gemfield@ThinkPad-X1C:~$ locate Python.h
/home/gemfield/anaconda3/include/python3.7m/Python.h
/home/gemfield/anaconda3/pkgs/python-3.7.6-h0371630_2/include/python3.7m/Python.h
/usr/include/python3.8/Python.h

如果找不到,則安裝 python-dev 或 python3-dev; 否則包括編譯器的正確頭文件路徑:

g++ -I/usr/include/python3.8 ...

我在Ubuntu上。 我已經按照一些答案中的建議安裝了所有軟件包。

sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

我仍然有這個問題,這條線:

#include "Python.h"

還有一些,我可以手動編輯它們,這是一種不好的做法。 我現在知道了這個秘密,它來自cython源代碼。 我有文件。 它編譯沒有錯誤。 那是文件。 將 PYTHON 更改為您擁有的 python 版本,python/python3。 將 FILE 更改為您的 c 文件名。 makefile 文件的名稱應該是Makefile 使用以下命令運行文件:

make all

用於創建我們的獨立 Cython 程序的 Makefile

    FILE := file.c
    PYTHON := python3
    PYVERSION := $(shell $(PYTHON) -c "import sys;                     
    print(sys.version[:3])")
    PYPREFIX := $(shell $(PYTHON) -c "import sys; print(sys.prefix)")

    INCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig; 
    print(sysconfig.get_python_inc())")
    PLATINCDIR := $(shell $(PYTHON) -c "from distutils import 
    sysconfig; print(sysconfig.get_python_inc(plat_specific=True))")
    LIBDIR1 := $(shell $(PYTHON) -c "from distutils import sysconfig; 
    print(sysconfig.get_config_var('LIBDIR'))")
    LIBDIR2 := $(shell $(PYTHON) -c "from distutils import sysconfig; 
    print(sysconfig.get_config_var('LIBPL'))")
    PYLIB := $(shell $(PYTHON) -c "from distutils import sysconfig; 
    print(sysconfig.get_config_var('LIBRARY')[3:-2])")

    CC := $(shell $(PYTHON) -c "import distutils.sysconfig; 
    print(distutils.sysconfig.get_config_var('CC'))")
    LINKCC := $(shell $(PYTHON) -c "import distutils.sysconfig; 
    print(distutils.sysconfig.get_config_var('LINKCC'))")
    LINKFORSHARED := $(shell $(PYTHON) -c "import distutils.sysconfig; 
    print(distutils.sysconfig.get_config_var('LINKFORSHARED'))")
    LIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; 
    print(distutils.sysconfig.get_config_var('LIBS'))")
    SYSLIBS :=  $(shell $(PYTHON) -c "import distutils.sysconfig; 
    print(distutils.sysconfig.get_config_var('SYSLIBS'))")

    .PHONY: paths all clean test

    paths:
        @echo "PYTHON=$(PYTHON)"
        @echo "PYVERSION=$(PYVERSION)"
        @echo "PYPREFIX=$(PYPREFIX)"
        @echo "INCDIR=$(INCDIR)"
        @echo "PLATINCDIR=$(PLATINCDIR)"
        @echo "LIBDIR1=$(LIBDIR1)"
        @echo "LIBDIR2=$(LIBDIR2)"
        @echo "PYLIB=$(PYLIB)"
        @echo "CC=$(CC)"
        @echo "LINKCC=$(LINKCC)"
        @echo "LINKFORSHARED=$(LINKFORSHARED)"
        @echo "LIBS=$(LIBS)"
        @echo "SYSLIBS=$(SYSLIBS)"

    $(FILE:.c=): $(FILE:.c=.o)
        $(LINKCC) -o $@ $^ -L$(LIBDIR1) -L$(LIBDIR2) -l$(PYLIB)         
    $(LIBS) $(SYSLIBS) $(LINKFORSHARED)

    $(FILE:.c=.o): $(FILE)
        $(CC) -c $^ -I$(INCDIR) -I$(PLATINCDIR)

    all: $(FILE:.c=)

當我嘗試使用 Python3.6 在 CentOS 7 上安裝 ctds 時出現此錯誤。 我做了這里提到的所有技巧,包括yum install python34-devel 問題是Python.h在 /usr/include/python3.4m 中找到, /usr/include/python3.4m but not in /usr/include/python3.6m中沒有。 我嘗試使用--global-option指向包含目錄( pip3.6 install --global-option=build_ext --global-option="--include-dirs=/usr/include/python3.4m" ctds ) . 這導致鏈接lpython3.6m時找不到lpython3.6m。

最后起作用的是修復 Python3.6 的開發環境需要使用包含和庫進行更正。

yum -y install https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/python36u-libs-3.6.3-1.ius.centos7.x86_64.rpm

Python.h 需要在 gcc 的包含路徑中。 無論使用哪個版本的 python,例如如果它是 3.6,那么它通常應該在/usr/include/python3.6m/Python.h中。

當您嘗試刪除python3.5並安裝python3.6時,它經常出現。

所以在使用python3 (其中python3 -V => python3.6 )安裝一些需要python3.5頭的包時會出現這個錯誤。

通過安裝python3.6-dev模塊解決。

有時即使在安裝 python-dev 之后錯誤仍然存​​在,如果缺少“gcc”,請檢查錯誤。

首先按照https://stackoverflow.com/a/21530768/8687063中的說明下載,然后安裝 gcc

對於 apt(Ubuntu、Debian ...):

sudo apt-get install gcc

對於百勝(CentOS,RHEL ...):

sudo yum install gcc

對於 dnf(Fedora ...):

sudo dnf install gcc

對於 zypper (openSUSE...):

sudo zypper in gcc

對於 apk(高山...):

sudo apk gcc

這意味着Python.h不在編譯器的默認包含路徑中。 您是在系統范圍內還是在本地安裝它? 你的操作系統是什么?

您可以使用-I<path>標志來指定編譯器應在其中查找標頭的附加目錄。 您可能必須跟進-L<path>以便 gcc 可以找到您將使用-l<name>鏈接的庫。

確認如果你運行的是centos 8+。 您將需要運行以下命令:

sudo yum -y install python36 python38 python39
sudo yum -y install python36-devel.x86_64 python38-devel.x86_64 python39-devel.x86_64

pip3.8 install cpython
pip3.9 install cpython
pip3.6 install cpython

如果你使用 cmake 來構建項目,你可以使用這個例子。

cmake_minimum_required(VERSION 2.6)
project(demo)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(demo main.cpp)
target_link_libraries(demo ${PYTHON_LIBRARIES})

我遇到的情況是我的Python.h在目錄/usr/include/python3.8/usr/include/python2.7 ,只要給gcc的路徑like -I /usr/include/python3.8 。python的版本替換為你的。

如果您已經安裝了 Python,並且想要鏈接到特定的 Python 版本,您可以從 Python 獲取相關的包含路徑。

>>> import sysconfig
>>> sysconfig.get_path('include')
'/usr/include/python3.10' # Example output

您可以使用 shell 腳本針對特定的 python 版本手動指定包含路徑/構建:

#!/bin/sh
python_include=$(python3.10 -c "import sysconfig; print(sysconfig.get_path('include'))")
gcc -Wall utilsmodule.c -o Utilc -I"$python_include"

安裝 python-dev python3-dev 后,仍然報告此錯誤。 所以我強制設置 header 路徑。 在我的系統中, python 3 header 路徑是 /usr/include/python3.8 所以 C_INCLUDE_PATH=/usr/include/python3.8/:/usr/include:/usr/local/include:/tmp EXPORT C_INCLUDE_PATH 編譯通過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM