1

I am creating a Python module using C, though when I use #include <Python.h> it says that it find this file, even though I have added it to the user PATH variable.

If I use #include "C:/Users/<my user>/AppData/Local/Programs/Python/Python38/include/Python.h" it works on my PC, though this leads to errors when importing via PyPi, so I would like the first method to work.

Any ideas as to how I can fix this would be greatly appreciated!

5
  • 4
    You neglected to mention which C compiler you're using, but I don't think any of them use the PATH variable to determine their include directory search pattern. Commented Aug 13, 2020 at 16:41
  • 2
    The PATH environment variable is the search path for executables, not for C include files. Commented Aug 13, 2020 at 16:53
  • I forgot to mention that the error is in CLion, not when compiling it and uploading it. Sorry about this... Commented Aug 13, 2020 at 16:55
  • The Microsoft C compiler supports command-line arguments that specify where to look for header files, so the one for the Python header files may be missing (has nothing to do with the PATH environment variable). Commented Aug 13, 2020 at 16:55
  • 1
    Doesn't CLion have project settings for setting include file paths? Commented Aug 13, 2020 at 16:56

2 Answers 2

2

I don't know of any compilers that use PATH to find include files. Instead, they use a separate environment variable such as C_INCLUDE_PATH. You need to find the correct environment variable to set.

You can almost certainly set this in CLion's settings somewhere, either globally or for the specific project. Check out their help file: https://www.jetbrains.com/help/clion/managing-included-files.html and https://www.jetbrains.com/help/clion/absolute-path-variables.html

Alternatively, all compilers have a command-line option to specify the include search path. For gcc, use -I.

Sign up to request clarification or add additional context in comments.

Comments

0

I found out a way to get around this issue by simply using the preprocessor and CLion specific definitions:

#ifdef __CLION_IDE_
#include "C:/Users/<user>/AppData/Local/Programs/Python/Python38/include/Python.h"
#include "C:/Users/<user>/AppData/Local/Programs/Python/Python38/include/structmember.h"
#else
#include <Python.h>
#include <structmember.h>
#endif

which will use the specific path in CLion and the header when building the python module.

4 Comments

That's not a macro, you're using the C compiler's preprocessor.
Rather than use the full path, you need to determine the correct enviornment variable to include the path in.
@Code-Apprentice: Why exactly is using an environment variable to specify the include-file path preferable (assuming CLion supports it).?
@martineau It is more flexible, say if the OP wants to build against a different version of Python, then the only change is to an envvar, and not to code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.