Как запустить скрипт Python из C++ в виртуальной среде Python? ⇐ Python

Программы на Python
Anonymous
Как запустить скрипт Python из C++ в виртуальной среде Python?

Сообщение Anonymous »

Я пытаюсь запустить простой скрипт Python:

Код: Выделить всё

import sys
from miditok import TokSequence

if __name__ == "__main__":

# Print out all arguments
print(type(sys.argv))
print("Tokens Received:")
for idx, arg in enumerate(sys.argv):
print(f"Argument {idx}: {arg}")

input_toksequence = TokSequence()

input_toksequence.tokens = sys.argv
из моего приложения на C++:

Код: Выделить всё

#define PY_SSIZE_T_CLEAN
#include

#include 
#include 
#include 
#include 
#include 
#include 

int main(){

FILE* f = fopen("C:/Users/rizzo/Desktop/TESI/dependencies_test/MMM/tests/test_real_time/generate_maestral.py", "r+");

if (f == NULL) {
printf("Error opening file.\n");
return 1;
}

// Initialize the Python Interpreter
Py_Initialize();

std::wstring_convert converter;

std::vector my_args = { "", "", "TOK_3", "TOK_4" };

// Create a vector of wide-character strings (wchar_t*)
std::vector wstr_args;
std::vector wargv;

for (const auto& arg : my_args) {
// Convert each argument to std::wstring
std::wstring warg = converter.from_bytes(arg);
// Store the wide string
wstr_args.push_back(warg);
// Add the wchar_t* to the argv array
wargv.push_back(const_cast(wstr_args.back().c_str()));
}

PySys_SetArgvEx(wargv.size(), wargv.data(), 0);

PyRun_SimpleFile(f, "ayyy");

fclose(f);
// Finish the Python Interpreter
Py_Finalize();

return 0;
}
Я могу заставить его работать, если в скрипт Python не импортирована внешняя библиотека, что мне и нужно в моем случае.
Я получаю следующую ошибку:

Код: Выделить всё

  File "ayyy", line 2, in 
ModuleNotFoundError: No module named 'miditok'
Можно ли активировать виртуальную среду Python из моего кода на C++?

Подробнее здесь: https://stackoverflow.com/questions/790 ... nvironment

Вернуться в «Python»