Hello developers,
I wanted to ask a question concerning a topic that might be of general interest to developers of plugins. I am trying to write a psi4 plugin with a python interface, that is, a python module that make classes and functions in my plugin available in a psi4 input file. To start I created a new plugin called py_interface
. Then, I tried to implement a simple function that returns a char* and link it to python:
#include <boost/python.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "forte.h"
using namespace boost;
using namespace boost::python;
using namespace psi;
namespace psi{ namespace py_interface {
extern "C"
int read_options(std::string name, Options& options)
{
if (name == "PY_INTERFACE"|| options.read_globals()) {
/*- The amount of information printed to the output file -*/
options.add_int("PRINT", 1);
}
return true;
}
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE (py_interface) // Attempt #1: create module (possible name conflict?)
//void export_py_interface() // Attempt #2: export interface
{
def("greet", greet);
}
extern "C"
SharedWavefunction py_interface(SharedWavefunction ref_wfn, Options& options)
{
int print = options.get_int("PRINT");
// void export_py_interface() // Attempt #2: export interface
return ref_wfn;
}
}} // End namespaces
The following call in a psi4 input fails:
print py_interface.greet()
>>> An error has occurred. Traceback:
>>> <type 'exceptions.AttributeError'>: 'module' object has no attribute 'greet': File "<string>", line 32, in <module>
In my second attempt, I exported directly the interface without creating a module (see comments in above code). In this case the input
energy('py_interface')
print py_interface.greet()
fails during the call to export_py_interface()
in the function py_interface
:
An error has occurred. Traceback:
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute '__dict__': File "<string>", line 29, in <module>
File "/Users/francesco/Source/psi4/share/python/driver.py", line 570, in energy
wfn = procedures['energy'][lowername](lowername, molecule=molecule, **kwargs)
File "./../py_interface/pymodule.py", line 66, in run_py_interface
py_interface_wfn = psi4.plugin('py_interface.so', ref_wfn)
This leads me to wonder if it is actually possible at all to export plugins functions as python functions.
I would greatly appreciate any suggestion or solution.