googletrans
Pythonライブラリを使用してフレーズを翻訳する単純なc ++アプリケーションを作成しようとしています。そのため、Pythonを埋め込むためにこの目的でpybind11を選択しました。また、コード構成にはcmakeを使用しています。
グローバルなPythonインストールを使用している場合はすべて正常に機能しますが、pybindで仮想環境を使用する方法と、cmakeで正しいPythonインタープリターやパスなどを設定するプロセス全体を理解していません。
私はこのstackoverflowスレッドを見つけました:仮想環境でのpybind11の埋め込み
@ipaと同じようにすべての変数を設定しました。ただ、見てCMakeLists.txt
とmain.cpp
:
cmake_minimum_required(VERSION 3.15)
project("cpp_google_trans")
set(
PYTHON_EXECUTABLE "${CMAKE_HOME_DIRECTORY}/venv/Scripts/python.exe"
CACHE FILEPATH "python virtual environment executable")
message(STATUS "PYTHON_EXECUTABLE is now: ${PYTHON_EXECUTABLE}") #DEBUG
set(ENV{PYTHONPATH} "${CMAKE_HOME_DIRECTORY}/venv/Lib/site-packages")
message(STATUS "ENV{PYTHONPATH} is now: $ENV{PYTHONPATH}") #DEBUG
set(
ENV{PATH}
"${CMAKE_HOME_DIRECTORY}/venv/Scripts/;${CMAKE_HOME_DIRECTORY}/venv/Lib"
)
message(STATUS "PATH is now: $ENV{PATH}") #DEBUG
add_subdirectory(pybind11)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE pybind11::embed)
main.cpp
:
#include <iostream>
#include <string>
#include <pybind11/embed.h>
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{};
auto sys = py::module::import("sys");
py::print("Hello, World from Python!");
py::print(sys.attr("executable"));
py::print(sys.attr("version"));
system("set PATH");
std::cin.get();
// system("powershell");
// I was looking at the environment variables in the powershell, but there wasn't any variables I set in CMakeLists.txt
// Here I get abort() when trying to import googletrans package.
// The package is installed in the venv virtual environment.
// When the package is installed globally there isn't any problem.
py::object Translator = py::module::import("googletrans").attr("Translator");
py::object translator = Translator();
std::cout << "C++ program here!\nTell a phrase you want to translate to english: ";
std::string phrase;
std::getline(std::cin, phrase);
std::cout << "\n\nThe phrase in english means: ";
std::cout << translator.attr("translate")(py::cast(&phrase)).attr("text").cast<std::string>();
}
cmake構成の出力を見ると、すべてがうまく機能しているように見えます(PythonLibs変数の代わりに、変更方法がわかりません)。
-- Building for: Visual Studio 16 2019
-- The C compiler identification is MSVC 19.22.27812.2
-- The CXX compiler identification is MSVC 19.22.27812.2
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/VC/Tools/MSVC/14.22.27812/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/VC/Tools/MSVC/14.22.27812/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/VC/Tools/MSVC/14.22.27812/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/VC/Tools/MSVC/14.22.27812/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- PYTHON_EXECUTABLE is now: D:/dev/Pybind/cpp_google_trans2/venv/Scripts/python.exe
-- ENV{PYTHONPATH} is now: D:/dev/Pybind/cpp_google_trans2/venv/Lib/site-packages
-- PATH is now: D:/dev/Pybind/cpp_google_trans2/venv/Scripts/;D:/dev/Pybind/cpp_google_trans2/venv/Lib
-- Found PythonInterp: D:/dev/Pybind/cpp_google_trans2/venv/Scripts/python.exe (found version "3.7.3")
-- Found PythonLibs: D:/Programs/VisualStudio/Shared/Python37_64/libs/Python37.lib
-- pybind11 v2.3.dev1
-- Configuring done
-- Generating done
-- Build files have been written to: D:/dev/Pybind/cpp_google_trans2/build
またcontrol panel>system and security>system>advanced system settings>advanced>environment variables
、を介してPYTHON_EXECUTABLEおよびPATH環境変数をグローバルに設定しようとしましたが、これでも役に立ちませんでした。
私はまだcmake、python、pybind11にかなり慣れていませんが、多くのことを知らないことを認識しています。そして多分あなたはこのスレッドを読んでいる間にこれに気づいたでしょう。; P
このソリューションには2つの部分があります。
virtualenvPYTHONPATH
をC ++プログラムにコンパイルします。CMakeでは
、これはで実行できますtarget_compile_definitions
。カスタムパスは、プリプロセッサマクロとして使用できます。
target_compile_definitions(app PRIVATE -DCUSTOM_SYS_PATH="\"${CMAKE_HOME_DIRECTORY}/venv/Lib/site-packages\"")
sys.path
インタープリターを起動してPythonを更新します。
これはpybind固有ですが、次のようになります。
py::module sys = py::module::import("sys");
sys.attr("path").attr("insert")(1, CUSTOM_SYS_PATH);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加