C++ calls Python in Anaconda3 error: ModuleNotFoundError: No module named 'zlib'

C++ calls Python in Anaconda3 error: ModuleNotFoundError: No module named 'zlib'

我需要用C++代码调用Python代码(使用了TensorFlow),Python是Anaconda3安装的。但是我发现我的代码无法加载python代码,因为tensorflow模块没有加载成功。清楚的问题在下面。

我的 C++ 代码:

#include <iostream>
#include <python.h>

int main()
{
    Py_Initialize();
    PyRun_SimpleString("import tensorflow");
    Py_Finalize();
    std::cout << "Hello World!\n";
}

输出:

2021-08-03 00:23:24.264014: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module>
    from tensorflow.python import data
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 134, in <module>
    from tensorflow.python.data.experimental.ops.readers import CsvDataset
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\experimental\ops\readers.py", line 23, in <module>
    import gzip
  File "C:\ProgramData\Anaconda3\lib\gzip.py", line 9, in <module>
    import zlib
ModuleNotFoundError: No module named 'zlib'
Hello World!

我用了base conda env,它有zlib模块,我在Python测试过没问题:

(base) PS C:\windows\system32> python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2021-08-03 00:26:11.966844: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
>>>

这是我的机器配置:

OS: Windows 10
IDE: VS 2019
Conda version: 4.10.3
Python version: 3.8.8
VS project configuration: 
    Additional Include Directories: C:\ProgramData\Anaconda3\include
    Additional Library Directories: C:\ProgramData\Anaconda3\libs
    Additional Dependencies: python38.lib

更新:环境变量

PYTHONHOME=C:\ProgramData\Anaconda3
PYTHONPATH is not set.
PATH=C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;C:\ProgramData\Anaconda3\condabin\;C:\ProgramData\Anaconda3\Library\bin;...
#include <iostream>
#include <python.h>

int main()
{
    Py_Initialize();
    PyRun_SimpleString("import os");
    PyRun_SimpleString("print(os.environ['PYTHONHOME']) ");
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("print(sys.path)");
    //PyRun_SimpleString("import tensorflow");
    Py_Finalize();
    std::cout << "Hello World!\n";
}

这是输出:

C:\ProgramData\Anaconda3
['C:\Users\dqs\source\repos\test\x64\Release\python38.zip', 'C:\ProgramData\Anaconda3\DLLs', 'C:\ProgramData\Anaconda3\lib', 'C:\Users\dqs\source\repos\test\x64\Release', 'C:\ProgramData\Anaconda3', 'C:\ProgramData\Anaconda3\lib\site-packages', 'C:\ProgramData\Anaconda3\lib\site-packages\locket-0.2.1-py3.8.egg', 'C:\ProgramData\Anaconda3\lib\site-packages\win32', 'C:\ProgramData\Anaconda3\lib\site-packages\win32\lib', 'C:\ProgramData\Anaconda3\lib\site-packages\Pythonwin']

为了避免混淆,因为报告的错误消息与通常报告的导入库和链接库之间的冲突不同。 我正在为这个特定问题添加这个答案,(ModuleNotFoundError: No module named 'zlib')

在这种情况下,用于构建可执行文件的 dll 库和路径环境变量引用的库是不同的。 为避免此冲突,请删除任何 python dll、headers 并从 vcpkg 安装中导入库。

vcpkg remove <pkg>

这里的pkg指的是安装在vcpkg中的python版本