0

This piece of code is part of one of my wxFrames for a sign up page. I am trying to run some Python code from C++ in order to switch frames, depending on the output of the Python file.

(I use this: namespace pb = pybind11; in order to refer to pybind11 just by pb)

I have done some debugging and find that this line of code is causing a problem.

pb::module python_module = pb::module::import("python_functions");

No error is returned, the program just doesn't continue.

I have spent all day trying to fix this bug and I am now out of ideas. The name of the Python file is correct, the Python file is in the same directory as the VS project, I have changed loads of PATHS in order for C++ to run the Python interpreter (maybe I've missed something??)

I have tested the Python code by itself and it works absolutely perfectly so it can't be the Python it's definitely that line of code. Here is the rest of the code:

std::string python_username_input = input_values[0].ToStdString();
                                                                    // convert the inputs into a string that Python can interpret
std::string python_password_input = input_values[1].ToStdString();

pb::scoped_interpreter guard{};

pb::module sys = pb::module::import("sys");       // Import sys module

sys.attr("path").attr("append")("[DIRECTORY TO FILE]");

pb::module python_module = pb::module::import("python_functions");

pb::object python_result = python_module.attr("SignUp")(python_username_input, python_password_input, check_values[0]);

Any help would be appreciated, this is for a project and I really need to get this sorted out. If you think you may need more context/code in order to work out the problem then please let me know. Maybe I need to #include another library? I have included '#include <pybind11/embed.h>' so maybe I'm missing something else?

Anyway, thank you for any response.

3
  • IIRC, in our project we just gave up and just went with py::eval everything from importing to actually executing. I think the problem is that py::module::import is not using the interpreter, so it doesn't see your modification to sys.path, but the function doesn't document how the lookup is performed. Commented Jan 25 at 20:09
  • Thanks for that, py::eval isn't a bad idea but I need to call a specific function which I am unable to do with py::eval. Any other ideas? Commented Jan 25 at 20:32
  • @Calax, you tested python code on the same machine where you tried the VS project? Maybe there is an incompatibility with python version? Commented Jan 26 at 3:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.