Psycopg2 on PBS hook

Hi all,

I’ve tried to use PBS hook and psycopg2 python library to write jobs’ data and users’ data to postgres database.

The problem is when I let the event trigger be “queuejob,modifyjob,resvsub,movejob,runjob”, psycopg2 library is imported with no problem. But when I try the event trigger “execjob_begin,execjob_prologue,execjob_epilogue,execjob_preterm,execjob_end,exechost_periodic,execjob_launch,exechost_startup,execjob_attach” pbs_python can not import the library.

The code which I’ve tried:
try:
import psycopg2
except:
file.write("###########Can not import the module #################\n")

How can I solve this problem.
Thank for any help.

Please make sure you can import psycopg2 on your system python both on server and compute nodes

In my environment:

[root@pbs ~]# ls /usr/lib64/python2.7/site-packages/psycopg2
errorcodes.py   extras.py     _json.py   psycopg1.py   _range.pyo
errorcodes.pyc  extras.pyc    _json.pyc  psycopg1.pyc  tz.py
errorcodes.pyo  extras.pyo    _json.pyo  psycopg1.pyo  tz.pyc
extensions.py   __init__.py   pool.py    _psycopg.so   tz.pyo
extensions.pyc  __init__.pyc  pool.pyc   _range.py
extensions.pyo  __init__.pyo  pool.pyo   _range.pyc

Modify and add the below code to your hook:

import os,sys
lib_path_python27='/usr/lib64/python2.7/site-packages'
if lib_path_python27 not in sys.path:
    sys.path.append(lib_path_python27)
    print sys.path
    import psycopg2
    dir(psycopg2)

To test this, just run the code using pbs_python abovecode.py , if this works, then
it should work in your hook as well.

Thank you