在哪里将.py放在C ++项目中,以便我可以使用pybind11进行访问?

时间:2019-04-02 16:25:51

标签: python c++ embed pybind11

案例

我在Visual Studio中创建了一个名为“ EmbedPython”的C ++项目。结果,我想用pybind11将Python API嵌入C ++。

我的Visual Studio项目

出于测试目的,我使用方法添加创建了自己的模块calc ,并将其放入了同一文件夹中。可以使用calc.py导入。

py::object calc = py::module::import("calc");

我想要的API的代码

#include <pybind11/embed.h> // everything needed for embedding
#include <iostream>
#include <exception>

namespace py = pybind11;

int main()
{
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive

    try
    {
        py::object pyObj = py::module::import("instagram_private_api").attr("Client").attr("ClientCompatPatch");
    }
    catch (std::exception &e)
    {
        std::cout << e.what() << std::endl;
    }


    int i;
    std::cin >> i;
}

问题

出现

访问冲突阅读...。我该怎么办?

这是应该有用的pybind11文档:https://pybind11.readthedocs.io/en/...object.html#accessing-python-libraries-from-c

错误代码

['F:\\Programmieren\\C++\\meine C++ - Projekte\\EmbedPython\\x64\\Release\\python36.zip', 'F:\\Programmieren\\C++\\meine C++ - Projekte\\EmbedPython\\EmbedPython\\DLLs', 'F:\\Programmieren\\C++\\meine C++ - Projekte\\EmbedPython\\EmbedPython\\lib', 'F:\\Programmieren\\C++\\meine C++ - Projekte\\EmbedPython\\x64\\Release', '.']
AssertionError: SRE module mismatch

At:
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\sre_compile.py(17): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\re.py(123): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\tokenize.py(33): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\linecache.py(11): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\traceback.py(5): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\EmbedPython\lib\logging\__init__.py(26): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\x64\Release\instagram_private_api\client.py(8): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load
  F:\Programmieren\C++\meine C++ - Projekte\EmbedPython\x64\Release\instagram_private_api\__init__.py(3): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(678): exec_module
  <frozen importlib._bootstrap>(665): _load_unlocked
  <frozen importlib._bootstrap>(955): _find_and_load_unlocked
  <frozen importlib._bootstrap>(971): _find_and_load

其他

我对Python的理解是,模块只是一个.py,其内容就像一个类。因此,将调用带有方法add的模块calc。这是正确的吗?

import calc
calc.add()

然后Python API中的instagram_private_api.py在哪里?

关于我

主要,我使用C ++开发。我做了Python几天,只是因为我需要上面的API。

0 个答案:

没有答案