0

#include <Python.h> the error of this code is 'Python.h' file not found.

I've installed python by brew, the results show below. And the path of python already written in to $PATH

MacBook-Pro test % python --version
Python 3.10.8
MacBook-Pro test % brew search python
==> Formulae
app-engine-python                    python-tk@3.11
boost-python3                        python-tk@3.9
bpython                              python-typing-extensions
gst-python                           python-yq
ipython                              python@3.10 ✔
libpython-tabulate                   python@3.11
micropython                          python@3.7
ptpython                             python@3.8
python-build                         python@3.9
python-gdbm@3.11                     reorder-python-imports
python-launcher                      wxpython
python-lsp-server                    pythran
python-markdown                      jython
python-tabulate                      cython
python-tk@3.10

==> Casks
awips-python                         mysql-connector-python

If you meant "python" specifically:
It was migrated from homebrew/cask to homebrew/core.
MacBook-Pro test % echo $PATH
/usr/local/opt/python@3.10/libexec/bin:/Users/fanxuezhou/opt/anaconda3/bin:/Users/fanxuezhou/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

If someone can tell me how to use Python.h in Xcode, or how can I compile the code by command line?

MacBook-Pro test % gcc main.cpp -o test 
main.cpp:9:10: fatal error: 'Python.h' file not found
#include <Python.h>
         ^~~~~~~~~~
1 error generated.

The error message of Xcode

  • 2
    Isn't this a duplicate of your question posted yesterday?https://stackoverflow.com/questions/74443972/python-h-no-such-file-or-directory-for-macbook – Sembei Norimaki Nov 16 '22 at 10:09
  • @SembeiNorimaki Yes, more detail. I really don't know what should I do. – LoveSherlock Nov 16 '22 at 10:12
  • This accepted answer seems to provide the location of Python.h for Mac. Have you already tried this? https://stackoverflow.com/questions/2686520/unable-to-build-mercurial-on-osx-python-h-not-found – Sembei Norimaki Nov 16 '22 at 10:15
  • 2
    `$PATH` is where the system looks for executable files. It has no effect on where the compiler looks for headers or libraries. Read about the `-I` and `-L` compiler options. (And you should build with g++, not gcc.) – molbdnilo Nov 16 '22 at 11:04
  • @molbdnilo FANdeMacBook-Pro test % g++ main.cpp -o test -I /System/Volumes/Data/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/include/python3.10 -l python3.10 ld: library not found for -lpython3.10 clang: error: linker command failed with exit code 1 (use -v to see invocation) – LoveSherlock Nov 17 '22 at 01:39
  • @molbdnilo I try to use g++, it seems found 'python.h', but still have some invocation problem. – LoveSherlock Nov 17 '22 at 01:40
  • @SembeiNorimaki Here's what I got based on the method of that accepted answer,`FANdeMacBook-Pro test % g++ main.cpp -o test -I /System/Volumes/Data/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/include/python3.10 Undefined symbols for architecture x86_64: "_Py_Finalize", referenced from: _main in main-e41c5e.o "_Py_Initialize", referenced from: _main in main-e41c5e.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)` – LoveSherlock Nov 17 '22 at 01:43
  • @SembeiNorimaki According to another answerer above, I should be missing the `-l `parameter, but the method I found on the Internet doesn't seem to work. – LoveSherlock Nov 17 '22 at 01:45

1 Answers1

0

I've fixed this problem by using g++,

g++ main.cpp -o test -I /usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/include/ -L /usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib  -l python3.10