c++ - g++ - Python.h: No such file or directory - TagMerge
6g++ - Python.h: No such file or directoryg++ - Python.h: No such file or directory

g++ - Python.h: No such file or directory

Asked 7 months ago
5
6 answers

I've set up a similar example on my github

g++ t.cpp is missing a few things:

  • Tell g++ where the headers are for cpython (by -I/path/to/headers/)
  • Tell g++ to link against libpython (by -lpython3.5m)

You can also retrieve these flags with pkg-config

$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m

Your commandline should look something like g++ -I/usr/include/python3.5m t.cpp -lpython3.5m

Source: link

0

#include <...> is for includes that come with the compiler.
Use #include "Python.h" for any other includes.

Source: link

0

Run the following commands to compile your code:

mytest.cpp:

#include <Python.h>

int main(int argc, char* argv[])
{
    Py_Initialize();
    PyRun_SimpleString("print('TEST PASSED')");
    Py_Finalize();
    
    return 0;
}

Compile:

$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest

Source: link

0

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:
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.
For apt (Ubuntu, 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...):
sudo yum install python-devel    # for python2.x installs
sudo yum install python3-devel   # for python3.x installs
For dnf (Fedora...):
sudo dnf install python2-devel  # for python2.x installs
sudo dnf install python3-devel  # for python3.x installs

Source: link

0

One of the common error that every python developer encounters is the following. This occurs while the installation of Python packages. This can be solved by installing few dependent Linux packages.
fatal error: Python.h: No such file or directory
RHEL or CentOS
sudo yum install python3-devel
Debian or Ubuntu
sudo apt-get install python3-dev
SUSE Linux
sudo zypper in python3-devel
Fedora Linux
sudo dnf install python3-devel

Source: link

0

I’m trying to make a C++ script that will run some simple Python code:
// t.cpp
#include <Python.h>

int main(int argc, char* argv[])
{
    Py_Initialize();
    PyRun_SimpleString("print('TEST PASSED')");
    Py_Finalize();

    return 0;
}
I’ve found many similar questions, all specific to an IDE or other development software, or were solved by installing python3-dev. The python3-dev package is already installed, and I even tried manually including the header when attempting to compile:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
You can also retrieve these flags with pkg-config
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m

Source: link

Recent Questions on c++

    Programming Languages