23#include <pybind11/embed.h>
24#include <pybind11/stl.h>
69cur_path = os.path.realpath(current_path)
71# This is broken on MacOS and give shamrock instead i don't know why ... stupid python ...
72#sysyexec = os.path.realpath(sys.executable)
73# So the fix is to check that the resolved path starts with base_exec_prefix
74# see https://docs.python.org/3/library/sys.html#sys.base_prefix
75sysprefix = os.path.realpath(sys.base_exec_prefix)
77#if cur_path != sysyexec:
78if not cur_path.startswith(sysprefix):
79 print("Current python is not the expected version, you may be using mismatched Pythons.")
80 print("Current path : ",cur_path)
81 #print("Expected path : ",sysyexec)
82 print("Expected prefix : ",sysprefix)
86namespace shambindings {
92 std::string cmd = binary_path +
" -c \"import sys;print(sys.path, end= '')\"";
100 "Shamrock configured with Python path : \n "
109 std::string modify_path = std::string(
"paths = ") +
get_pypath() +
"\n";
110 modify_path += R
"(import sys;sys.path = paths)";
111 py::exec(modify_path);
113 std::string pylib_path = shambindings::locate_pylib_path(do_print);
114 std::string modify_path_lib = std::string("sys.path.insert(0, \"") + pylib_path +
"\")\n";
115 py::exec(modify_path_lib);
119 std::vector<std::string> cpp_argv(argv, argv + argc);
120 py::module_::import(
"sys").attr(
"argv") = py::cast(cpp_argv);
125 py::scoped_interpreter guard{};
142 void run_py_file(std::string file_path,
bool do_print,
int argc,
char *argv[]) {
143 py::scoped_interpreter guard{};
152 py::eval_file(file_path);
void println(std::string_view sv)
Prints a string to the console followed by a newline.
std::string popen_fetch_output(const char *command)
Run a command and return the output.
Pybind11 include and definitions.
std::optional< std::string > runtime_set_pypath
value use to set the value of sys.path if set by the user at runtime
const char * configure_time_py_sys_path()
path of the script to generate sys.path
std::string get_pypath()
Retrieves the Python path to be used for the application.
const char * configure_time_py_executable()
path of the python executable that was used to configure sys.path
const char * run_ipython_src()
Script to run ipython.
std::string check_python_is_excpeted_version
Script to check that the python distrib is the expected one.
void setpypath_from_binary(std::string binary_path)
set the value of sys.path before init from the supplied binary
void start_ipython(bool do_print, int argc, char *argv[])
Start shamrock embded ipython interpreter.
void modify_py_sys_path(bool do_print)
Modify Python sys.path to point to one detected during cmake invocation.
void set_sys_argv(int argc, char *argv[])
set the value of sys.argv
void setpypath(std::string path)
set the value of sys.path before init
void run_py_file(std::string file_path, bool do_print, int argc, char *argv[])
run python runscript