How do I "Correctly" include a Python.h header in my C++ program in VSCODE?

1.4k views Asked by At

My Environment:

1- MacOs catalina.

2- VScode "Visual Studio Code"

3- PlatformIO extension (for Arduino Development in C/C++) mainly C++

4- Python3 and Python2 are both installed on my mac.

The Problem:

I was trying to embed a python application called "sr1.py" into my C++ application but I'm getting this error when hovering on the #inclde <python.h> squiggly red line:

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/ImCodeName7/Documents/PlatformIO/Projects/speech_recognision/src/main.cpp).C/C++(1696) cannot open source file "python.h"C/C++(1696)

CODE:

#include <Arduino.h>
#include <python.h>


int main()
{
    char filename[] = "sr1.py";
    FILE* fp;

    Py_Initialize();

    fp = _Py_fopen(filename, "r");
    PyRun_SimpleFile(fp, filename);

    Py_Finalize();
    return 0;
}

Please note I am still learning how to configure my environment so please bear with me. Thank you!

Edit1: The source file is mainly written in C++ (Or known as Arduino C actually!)

1

There are 1 answers

0
Jake On

Assuming that this is simply a result of not including the proper path in the c_cpp_properties.json file.

If you know the path where python is located all you need to do is add the following to your c_cpp_properties.json file:

{
    "configurations": [
        {
            "name": "FOO",
            "includePath": [
                "${workspaceFolder}/**",
                "/PATH/TO/DIR/CONTAINING-Python.h",
                // --- E.g. ---
                "/usr/include/python3.9/Python.h",
                // Or
                "/usr/include/python3.9"
            ],

        }
    ],
}

If you don't, then you have to do some digging. For other *NIX systems the process should be similar, for ubuntu see this post.

For example, if you use Anaconda or miniconda.

Python.h should be located in:

../foo/<ENV_NAME>/include/pythonX.Y

So you would change "/PATH/TO/DIR/CONTAINING/Python.h" to "../foo/<ENV_NAME>/include/pythonX.Y"

To find the file:

Find the folder where the python executable is located.

which python (or which python2 / python3)

This should produce a file path that looks something like this:

../foo/<ENV_NAME>/bin/python

Change your current working directory to <ENV_NAME>

cd ../foo/<ENV_NAME>

In <ENV_NAME> there should be an include directory and in the include directory there should be another directory named pythonX.Y. This is the directory where Python.h is found.

E.g.

ENV_NAME/
├─ bin/
├─ include/
│  ├─ python3.9/
│  │  ├─ Python.h
│  │  ├─ ...
├─ lib/
├─ share/
├─ var/
├─ .../

On mac, no conda with homebrew

A little more tricky and requires a little more digging. In my case I found it located at

/opt/homebrew/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/include/python3.9


For windows (without any package managers) the default location is:

C:\Users\Foo\AppData\Local\Programs\Python\PythonXY\include

However, if it is not there, it should be located at one of the following locations:

  1. %ProgramFiles%\Python X.Y
  2. %ProgramFiles(x86)%\Python X.Y
  3. %LocalAppData%\Programs\Python\PythonXY
  4. %LocalAppData%\Programs\Python\PythonXY-32
  5. %LocalAppData%\Programs\Python\PythonXY-64