0

I'm editing an open-source codebase which uses PyBind11, found here: https://github.com/erwincoumans/motion_imitation/blob/master/mpc_controller/mpc_osqp.cc

I wanted to add another public method and then call that method in Python in a different file. The method literally just returns an int, it's just meant to be a test for now. However, when I try to do so, I get the following error: AttributeError: 'mpc_osqp.ConvexMpc' object has no attribute 'test'.

I added a new line at the end to the following to the PyBind Module:

PYBIND11_MODULE(mpc_osqp, m) {
m.doc() = R"pbdoc(
    MPC using OSQP Python Bindings
    -----------------------
    .. currentmodule:: mpc_osqp

    .. autosummary::
       :toctree: _generate

)pbdoc";

  
py::enum_<QPSolverName>(m, "QPSolverName")
  .value("OSQP", OSQP, "OSQP")
  .value("QPOASES", QPOASES, "QPOASES")
  .export_values();
  
py::class_<ConvexMpc>(m, "ConvexMpc")
  .def(py::init<double, const std::vector<double>&, int,
      int , double ,const std::vector<double>&, double,QPSolverName>())
  .def("compute_contact_forces", &ConvexMpc::ComputeContactForces)
  .def("reset_solver", &ConvexMpc::ResetSolver     )
  .def("test", &ConvexMpc::Test); // THIS IS NEW

Where the final .def is the only edit I made. But it doesn't recognise the attribute .test

What am I missing? Thank you.

  • Basically, if I comment out the entire /mpc_osqp.cc file, the code can still be called. Which means that any edits I make to that file aren't registering. Do I have to make changes to CMake? But I can't find the CMake file in this repository... – McGinnFaeWin Sep 07 '22 at 20:39
  • Maybe you are still importing the unchanged module in python. `mpc_osqp.__file__` will tell you. – ChrisD Sep 07 '22 at 20:42
  • @ChrisD Hi, thank you! I think you're right. `mpc_osqp.__file__` returns `motion_imitation/pipEnv/lib/python3.7/site-packages/mpc_osqp.cpython-37m-x86_64-linux-gnu.so` , do you know how I can import the changed module? I'm not sure how to "re-load" it. The codebase was installed as a pip package with `pip3 install motion_imitation` , thank you – McGinnFaeWin Sep 07 '22 at 21:02
  • When you are developing a package the usual method is to install an "egg-link" rather copying the files into site-packages. This basically links the files you are working on into the python module search path so you can make changes. After uninstalling the existing package, run either `pip install -e .` or `python setup.py develop` in your working folder. Another option if you don't want to install it is `python setup.py build_ext --inplace` which is useful if it's just a single binary module. – ChrisD Sep 07 '22 at 21:23
  • @ChrisD Not sure if I understand correctly, but this is what I did I did: `pip uninstall motion_imitation` , then I did `pip install -e .` . When I ran the code again, I still got the same error: `AttributeError: 'mpc_osqp.ConvexMpc' object has no attribute 'test'.` To be honest I'm not very familiar with developing packages like this -- don't know much about site-packages or egg-links! Anything you can suggest or perhaps I misunderstood what you meant? Thank you for your help ! – McGinnFaeWin Sep 08 '22 at 00:59
  • Hrm. Check `mpc_osqp.__file__` again. "site-packages" is just the standard entry in the [module search path](https://realpython.com/lessons/module-search-path/) where python will look for external modules. An [egg](https://stackoverflow.com/questions/2051192/what-is-a-python-egg) is an old, superseded type of python package, but the name lives on in "egg-links" which are just a file in site-packages (or whereever) that links to a directory outside the module search path (typically a development directory). – ChrisD Sep 08 '22 at 04:13
  • Another tedious thing that can go wrong developing binary packages is that setuptools is not very good at detecting when things need to be rebuilt. Often the easiest thing to do is just remove the entire `./build` folder and then run setup.py (or pip install) again. – ChrisD Sep 08 '22 at 04:14

0 Answers0