Skip to content
Snippets Groups Projects
Commit e797b971 authored by Nils-Arne Dreier's avatar Nils-Arne Dreier
Browse files

initial commit

parents
Branches main
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.20)
project(embedded_python_dummy
LANGUAGES CXX)
find_package(Python3 REQUIRED COMPONENTS Development)
add_executable(embedded_python_dummy
embedded_python_dummy.cxx)
target_link_libraries(embedded_python_dummy Python3::Python)
#include <iostream>
#include <stdexcept>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main(int args, char** argv){
using namespace std::string_literals;
if(false){ // <- false is the current behavior in libpython_adapter
Py_Initialize();
}else{
PyStatus status;
PyConfig config;
PyConfig_InitPythonConfig(&config);
try{
config.site_import = 1; // <- en-/dis- able site module
status = PyConfig_SetBytesString(&config, &config.program_name,
argv[0]);
if (PyStatus_Exception(status)) {
throw status;
}
// set the path to the venvs executable
status = PyConfig_SetBytesString(&config, &config.executable,
"/home/nils/projects/comin/embedded_python_dummy/venv/bin/python3");
if (PyStatus_Exception(status)) {
throw status;
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
throw status;
}
}catch(...){
std::cerr << status.err_msg << std::endl;
}
PyConfig_Clear(&config);
}
PyObject* globals = PyDict_New();
FILE* file = fopen(argv[1], "r");
if (file == NULL)
throw std::runtime_error("Cannot read "s + std::string(argv[1]));
else {
PyObject* pyResult =
PyRun_File(file, argv[1], Py_file_input, globals, globals);
Py_XDECREF(pyResult);
fclose(file);
if (PyErr_Occurred()) {
PyErr_Print();
throw std::runtime_error("Error while executing "s +
std::string(argv[1]));
}
}
return 0;
}
import sys
print(f"{sys.prefix=}")
print(f"{sys.base_prefix=}")
print(f"{sys.exec_prefix=}")
print(f"{sys.executable=}")
print(f"{sys.path=}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment