#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #define IS_WINDOWS #include // Needed by GetModuleFileNameW #else #include // Needed by readlink #endif #include #include #ifndef IS_WINDOWS #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif #include #include #include #ifndef IS_WINDOWS #pragma GCC diagnostic pop #endif namespace py = pybind11; using namespace py::literals; ///============================================================================= #ifdef IS_WINDOWS std::wstring getExecutableDir() { wchar_t exePath[MAX_PATH]; GetModuleFileNameW(nullptr, exePath, MAX_PATH); const auto executableDir = std::wstring(exePath); const auto pos = executableDir.find_last_of('\\'); if (pos != std::string::npos) return executableDir.substr(0, pos); return L"\\"; } #else std::wstring getExecutableDir() { char result[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); if (count != -1) { const auto path = std::string(dirname(result)); return std::wstring(path.begin(), path.end()); } return L"/"; } #endif ///============================================================================= int main(int argc, char** argv) { (void)argc; (void)argv; // Get executable dir and build python PATH variable const auto exeDir = getExecutableDir(); #ifdef IS_WINDOWS const auto pythonPath = exeDir + L"\\lib;" + exeDir + L"\\app;"; #else const auto pythonPath = exeDir + L"/lib:" + exeDir + L"/app"; #endif // Set the environment variable for PYTHONPATH #ifdef IS_WINDOWS _wputenv_s(L"PYTHONPATH", pythonPath.c_str()); #else setenv("PYTHONPATH", std::string(pythonPath.begin(), pythonPath.end()).c_str(), 1); #endif std::wcout << "Python PATH set to: " << pythonPath << std::endl; try { py::scoped_interpreter guard{}; // Disable build of __pycache__ folders py::exec(R"( import sys sys.dont_write_bytecode = True )"); // Start Python REPL std::cout << "Starting Python REPL. Type your commands below:" << std::endl; py::exec(R"( import code code.interact(local=globals()) )"); } catch (std::exception& e) { std::cerr << "Something went wrong: " << e.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }