Why doesn't #include <Python.h> work?

23,707

Solution 1

I don't know much about python, but the message indicates that python27_d.lib either doesn't exist, or at least doesn't exist where the linker is looking for it.

You already fixed the compiler include issue, now find the python27_d.lib file with Windows Explorer and and add that path to the Additional Library Dependencies path. It's under Configuration -> Linker -> General -> Additional Library Directories.

The "_d" indicates it's a debug library, so you'll want that one for your Debug configuration, and the one without the "_d" (probably) for your release configuration.

Solution 2

I normally circumvent this by using the non-debug Python lib in debug builds. Typically, this leads to code like:

#ifdef _DEBUG
  #undef _DEBUG
  #include <Python.h>
  #define _DEBUG
#else
  #include <Python.h>
#endif

where you hide the definition of _DEBUG during the inclusion of Python.h.

Solution 3

Put visual studio in release mode instead of debug.

Solution 4

On Visual studio, you need to add 'Additional include directories' for the project. Steps below. right click on project -> properties -> c/c++ -> Additional Include Directories -> point it to 'Python\include' folder(ex: c:\python\include).

Solution 5

You don't necessarily have to use a Python debug build... [even if you are not usimng boost] I would have a look at the boost.python documentation where they have a wrapper for Python.h which handles all the windows debug issues, so that you can build a debug extension against a release python dll.

http://www.boost.org/doc/libs/1_53_0/libs/python/doc/building.html#id19 Python Debugging Builds

Share:
23,707
IssamLaradji
Author by

IssamLaradji

Updated on July 02, 2020

Comments

  • IssamLaradji
    IssamLaradji almost 4 years

    I'm trying to run Python modules in C++ using "#include <Python.h>", however, after setting the "Additional Include Dependencies" of the project to "\include" I get the following error when debuging,

    LINK : fatal error LNK1104: cannot open file 'python27_d.lib'
    

    I read that I should download the development version of Python, but I didn't find a link for that, plus, don't I just need the file 'python27_d.lib' to be copied to the "libs" folder?

    Please note that I'm using the Anaconda distribution of Python.

    Thanks in advance!

  • IssamLaradji
    IssamLaradji about 11 years
    Thanks, so how do I get the debug library?
  • Mark Stevens
    Mark Stevens about 11 years
    Ahh, sorry, thought it was just a VS issue. Looking around it appears that the release (no "_d") version is distributed, but not the "_d". People have found a couple ways to resolve it here: stackoverflow.com/questions/11311877/…
  • IssamLaradji
    IssamLaradji about 11 years
    I tried the solution "#define MS_NO_COREDLL" and it seems to be working thanks a million :), but I don't really understand why
  • Mark Stevens
    Mark Stevens about 11 years
    It appears that python.h explicitly links against debug or release libraries depending on preprocessor directives. I would just leave that to the project config.
  • rrs
    rrs almost 9 years
    It was really this answer combined with the one above that solved my issue. So the steps were: (1) set to release mode, not debug, (2) set the Linker to point to my Python path.
  • Jay Borseth
    Jay Borseth about 8 years
    DOH! This is the solution! Just make sure your debug version links with python27.lib instead of python27_d.lib.