我的Ubuntu系统上有Python,但是gcc找不到Python.h

use*_*344 49 c python ubuntu header

我在学校的电脑上,所以我无法安装任何东西.

我正在尝试创建可以在Python中运行的C代码.看来我发现的所有文章都要求你使用

#include <Python.h>
Run Code Online (Sandbox Code Playgroud)

我这样做,但是当我编译它时抱怨没有这样的文件或目录.

计算机有Python(至少它python在终端中有命令,我们可以运行我们想要的任何Python代码).

我输入locate Python.h了终端,但没有发现任何东西.

我有两个问题:

  1. 我可以编写可以在Python中调用的C代码Python.h吗?

  2. 我错过了什么,计算机实际上有Python.h吗?

小智 49

你需要python-dev包含的包Python.h

  • 我已经在ubuntu 12.04中安装了python-dev并获得了/usr/include/python2.7区域,但那里没有python.h文件.有任何想法吗? (6认同)
  • 某些情况下该包称为 python-devel x) (2认同)
  • 请参阅 danielcooperxyz 的答案:gcc $(python-config --includes) (2认同)
  • 或“python3.7-dev”,适用于 Python 3.7。 (2认同)

Sve*_*ach 42

在Ubuntu上,您需要安装一个名为的包python-dev.由于此软件包似乎没有安装(locate Python.h没有找到任何内容),并且您无法自己在系统范围内安装,因此我们需要一个不同的解决方案.

您可以在主目录中安装Python - 您不需要任何特殊权限来执行此操作.如果您被允许使用Web浏览器并运行gcc,这应该适合您.为此

  1. 下载源代码tarball.

  2. 解压缩

    tar xjf Python-2.7.2.tar.bz2
    
    Run Code Online (Sandbox Code Playgroud)
  3. 构建和安装

    cd Python-2.7.2
    ./configure --prefix=/home/username/python --enable-unicode=ucs4
    make
    make install
    
    Run Code Online (Sandbox Code Playgroud)

现在,您在主目录中安装了完整的Python.-I /home/username/python/include编译时传递给gcc以使其知道Python.h.通过-L /home/username/python/lib-lpython2.7链接时.

  • @JonathanHartley:不,我的意思正是我写的.你在源代码发行版中的路径`Lib /`与安装软件的路径`$ prefix/lib`混淆. (2认同)

小智 35

你必须使用#include"python2.7/Python.h"而不是#include"Python.h".


Mar*_*oma 26

对于Ubuntu 15.10和Python 3,由于他们没有Python.h管理权限,因此可以解决这个问题:

sudo apt-get install python-dev
sudo apt-get install python3-dev
sudo apt-get install libpython3-dev
sudo apt-get install libpython3.4-dev
sudo apt-get install libpython3.5-dev
Run Code Online (Sandbox Code Playgroud)

  • sudo apt-get install libpython3.6-dev为我解决了它。谢谢! (2认同)

dan*_*xyz 16

在ubuntu上,您只需键入sudo apt-get install python-dev -y终端即可安装python-dev软件包.


unu*_*tbu 6

头文件现在由libpython2.7-dev提供.

您可以使用packages.ubuntu.com上的搜索表单来查找包提供的内容Python.h.


小智 5

您需要安装python-dev。
对于Ubuntu的:
sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs
更多的发行版,请参阅-
/sf/answers/1507153791/


小智 5

我在ubuntuforums(ubuntuforums)中找到了答案,您可以将其添加到gcc'$(python-config --includes)'中

gcc $(python-config --includes) urfile.c
Run Code Online (Sandbox Code Playgroud)

  • 对于python3,它将是`python3-config` (3认同)