当前位置:首页 > 后端开发 > 致命错误:Python.h:没有这样的文件或目录

致命错误:Python.h:没有这样的文件或目录

6个月前 (05-25)43

本文翻译自:fatal error: Python.h: No such file or directory

I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below: 我正在尝试使用C扩展文件构建共享库,但首先我必须使用以下命令生成输出文件:

gcc -Wall utilsmodule.c -o Utilc

After executing the command, I get this error message: 执行命令后,我得到以下错误消息:

utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated. utilsmodule.c:1:20:致命错误:Python.h:没有此类文件或目录编译终止。

in fact I have tried all the suggested solutions over the internet but the problem still exists ... also I have no problem with Python.h . 实际上,我已经尝试了所有建议的解决方案,但是仍然存在该问题……对于Python.h我也没有问题。 I managed to locate the file on my machine ... anybody has faced the same problem before?? 我设法在我的机器上找到该文件……以前有人遇到过同样的问题吗??


#1楼

参考:https://stackoom.com/question/1SL5N/致命错误-Python-h-没有这样的文件或目录


#2楼

Looks like you haven't properly installed the header files and static libraries for python dev. 看来您尚未正确安装python dev的标头文件和静态库。 Use your package manager to install them system-wide. 使用软件包管理器在系统范围内安装它们。

For apt ( Ubuntu, Debian... ): 对于aptUbuntu,Debian ... ):

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

For yum ( CentOS, RHEL... ): 对于yumCentOS,RHEL ... ):

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

For dnf ( Fedora... ): 对于dnfFedora ... ):

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

For zypper ( openSUSE... ): 对于zypperopenSUSE ... ):

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

For apk ( Alpine... ): 对于apkAlpine ... ):

# 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

For apt-cyg ( Cygwin... ): 对于apt-cygCygwin ... ):

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

#3楼

This means that Python.h isn't in your compiler's default include paths. 这意味着Python.h不在编译器的默认包含路径中。 Have you installed it system-wide or locally? 您在系统范围内还是在本地安装了它? What's your OS? 您的操作系统是什么?

You could use the -I<path> flag to specify an additional directory where your compiler should look for headers. 您可以使用-I<path>标志指定编译器应在其中查找标头的其他目录。 You will probably have to follow up with -L<path> so that gcc can find the library you'll be linking with using -l<name> . 您可能必须跟上-L<path>以便gcc可以使用-l<name>找到要链接的库。


#4楼

Two things you have to do. 您必须做两件事。

Install development package for Python, in case of Debian/Ubuntu/Mint it's done with command: 为Debian / Ubuntu / Mint安装适用于Python的开发包,可通过以下命令完成:

sudo apt-get install python-dev

Second thing is that include files are not by default in the include path, nor is Python library linked with executable by default. 第二件事是,默认情况下,包含文件不在包含路径中,Python库也不与可执行文件链接。 You need to add these flags (replace Python's version accordingly): 您需要添加这些标志(相应地替换Python的版本):

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

In other words your compile command ought to be: 换句话说,您的编译命令应为:

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

#5楼

Make sure that the Python dev files come with your OS. 确保操作系统随附Python开发文件。

You should not hard code the library and include paths. 您不应该对库进行硬编码并包含路径。 Instead, use pkg-config, which will output the correct options for your specific system: 而是使用pkg-config,它将为您的特定系统输出正确的选项:

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

You may add it to your gcc line: 您可以将其添加到您的gcc行:

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

#6楼

I managed to solve this issue and generate the .so file in one command 我设法解决了这个问题,并在一个命令中生成了.so文件

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

作者:xfxf996
来源链接:https://blog.csdn.net/xfxf996/article/details/105306278

标签: Python

“致命错误:Python.h:没有这样的文件或目录” 的相关文章

不等于在python中怎么打

不等于在python中怎么打

在python中不等于用什么来表示十分简单,并且应用十分广泛。 在Python语言中,用 != 表示不等于。我们通过下面一个简单的例子来 理解:>>...

win10 安装python3,设置为默认python环境,pycharm,并安装常用模块

win10 安装python3,设置为默认python环境,pycharm,并安装常用模块

参考地址https://www.runoob.com/python3/python3-install.html   Python3 下载 Pyth...

Python:环境搭建(一)

Python:环境搭建(一)

文章目录 1.官网下载 2.安装 3.IDEA集成Python...

python-虚拟环境的配置

python-虚拟环境的配置

虚拟环境的搭建 优点 1、使不同应用开发环境相互独立 2、环境升级不影响其他应用,也不会影响全局的python环境 3、防止出现包管理混乱及包版本冲突 windows...

Windows10下CMD输入Python没反应的解决方案

Windows10下CMD输入Python没反应的解决方案

开始学习Python,第一步Hello world就把自己难住了,输了命令没有结果     后来发现Win10需要配置环境变量,执行下面语句...

python中的str.replace()函数

python中的str.replace()函数

python中字符串的元素替换函数,一般来说使用方法为 str.replace(old, new[, max]) 例如 s =...

Python2.x安装

Python2.x安装

python安装 1、简介 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的。由于之前代码都是2.x版本的, 这里也是以Pyth...

python3.6.0、ipython5.1.0安装配置环境变量

python3.6.0、ipython5.1.0安装配置环境变量

1.安装 python3.6.0(官网下载,自带安装pip),安装成功后。配置环境变量。 2.变量配置成功后,运行cmd,输入python运行,界面显示如下,就是安装成功了...

python开发环境配置与部署

python开发环境配置与部署

一. 安装python should I use Python 2 or Python 3 for my development a...

Mac 电脑安装 Python 环境

Mac 电脑安装 Python 环境

大家好啊,我是大田 2022.04.08,日更第 62 / 365 天。 今天来给大家分享下在 Mac 电脑上如何安装 Python 环境。...