为什么 #include <Python.h> 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16200997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:00:47  来源:igfitidea点击:

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

c++pythonvisual-studio-2010wrapper

提问by Curious

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,

我正在尝试使用 C++ 运行 Python 模块"#include <Python.h>",但是,在将项目的“附加包含依赖项”设置为“\include”后,调试时出现以下错误,

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?

我读到我应该下载 Python 的开发版本,但我没有找到链接,另外,我不是只需要将文件“python27_d.lib”复制到“libs”文件夹吗?

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

请注意,我使用的是 Python 的 Anaconda 发行版。

Thanks in advance!

提前致谢!

采纳答案by Mark Stevens

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.

我对 python 了解不多,但该消息表明 python27_d.lib 要么不存在,要么至少在链接器正在寻找它的地方不存在。

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.

您已经修复了编译器包含问题,现在使用 Windows 资源管理器找到 python27_d.lib 文件,并将该路径添加到附加库依赖项路径。它位于 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.

“_d”表示它是一个调试库,因此您需要将其用于调试配置,而将没有“_d”(可能)用于发布配置的库。

回答by seanv507

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.

您不一定必须使用 Python 调试版本... [即使您没有使用 boost] 我会查看 boost.python 文档,其中有 Python.h 的包装器,可处理所有 Windows 调试问题,以便您可以针对发布的 python dll 构建调试扩展。

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

http://www.boost.org/doc/libs/1_53_0/libs/python/doc/building.html#id19Python 调试构建

回答by user2551828

I ran into similar errors while attempting to use Boost.python to access Anaconda python packages from C++. Let me start off by saying that my personal impression of the C++ Boost libraries is that they are a great idea with incomplete documentation. There is a ton of documentation on boost.org, but it invariably seems to leave out critical details which the authors appear to consider too trivial to bother mentioning. But, let me get off my soapbox ...

我在尝试使用 Boost.python 从 C++ 访问 Anaconda python 包时遇到了类似的错误。首先让我说,我对 C++ Boost 库的个人印象是它们是一个很好的想法,但文档不完整。boost.org 上有大量文档,但它似乎总是遗漏了作者似乎认为太琐碎而无法提及的关键细节。但是,让我离开我的肥皂盒......

The ongoing impetus for me to [hopefully, eventually] figure out how to get Boost.Python to work on my system is that there are so many great Python scientific packages [SymPy, Numpy, SciPy, matplotlib, etc] included in the Anaconda distribution and it would really be great to access them from C++ projects built with Qt Creator. And the Boost docs do seem to suggest that Boost.Python is supposed to do that for me. Alas, those docs seem to leave out critical details that the authors appear to consider too trivial to bother mentioning ...

我 [希望最终] 找出如何让 Boost.Python 在我的系统上工作的持续动力是,Anaconda 发行版中包含了许多出色的 Python 科学包 [SymPy、Numpy、SciPy、matplotlib 等]从使用 Qt Creator 构建的 C++ 项目中访问它们真的很棒。Boost 文档似乎确实暗示 Boost.Python 应该为我做到这一点。唉,这些文档似乎遗漏了作者似乎认为太琐碎而懒得提及的关键细节......

Anyway, initially, I got a build error indicating that python.h could not be found. I got rid of that by adding these two statements to my Qt Creator project's .pro file, which tell qmake where Boost installed its include files and where Anaconda installed its python.h file on my system:

无论如何,最初,我收到一个构建错误,表明找不到 python.h。我通过将这两个语句添加到我的 Qt Creator 项目的 .pro 文件中来摆脱它,它告诉 qmake Boost 在哪里安装了它的包含文件,以及 Anaconda 在我的系统上安装了它的 python.h 文件:

INCLUDEPATH += C:\boost_1_55_0
INCLUDEPATH += C:\Anaconda\include

After that, I got a LNK1104 error indicating that 'python27.lib' could not be found. I got rid of that by adding these two statements to my .pro file. The first tells qmake where to find Anaconda's python27.lib file. The second tells qmake where to find the boost.python binary:

之后,我收到一个 LNK1104 错误,表明找不到“python27.lib”。我通过将这两个语句添加到我的 .pro 文件来摆脱它。第一个告诉 qmake 在哪里可以找到 Anaconda 的 python27.lib 文件。第二个告诉 qmake 在哪里可以找到 boost.python 二进制文件:

LIBS += "C:/Anaconda/libs"
LIBS += "C:/boost_1_55_0/stage/lib/libboost_python-vc110-mt-gd-1_55.lib"

But, that is as far as I have gotten so far. I now get an error indicating it cannot open file 'C:/Anaconda/libs.obj' which I have not yet found a fix for. The error, of course, is caused by that file not existing. The challenge is to discover why it is being sought and where to find it.

但是,这就是我到目前为止所得到的。我现在收到一个错误,表明它无法打开我尚未找到修复程序的文件“C:/Anaconda/libs.obj”。当然,错误是由该文件不存在引起的。挑战在于发现为什么要寻找它以及在哪里找到它。

回答by Santi Pe?ate-Vera

Put visual studio in release mode instead of debug.

将 Visual Studio 置于发布模式而不是调试模式。

回答by Klamer Schutte

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

我通常通过在调试版本中使用非调试 Python 库来规避这一点。通常,这会导致代码如下:

#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.

在包含 Python.h 期间隐藏 _DEBUG 的定义。

回答by G T

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).

在 Visual Studio 上,您需要为项目添加“其他包含目录”。下面的步骤。右键单击项目 -> 属性 -> c/c++ -> 附加包含目录 -> 将其指向“Python\include”文件夹(例如:c:\python\include)。