Ubuntu running Python connection PG, error Importerror:no module named PSYCOPG2
[Email protected]:~# python/home/zxw/pgwritertest_m.py
Traceback (most recent):
File "/home/zxw/pgwritertest_m.py", line 4, <module>
Import PSYCOPG2
Importerror:no module named PSYCOPG2
For example, the following installation:
1
[Email protected]:~# apt-cache Search PSYCOPG2
Python-psycopg2-python Module for PostgreSQL
Python-psycopg2-dbg-python Module for PostgreSQL (debug extension)
Python-psycopg2-doc-python Module for PostgreSQL (documentation package)
Python3-psycopg2-python 3 Module for PostgreSQL
Python3-psycopg2-dbg-python 3 Module for PostgreSQL (debug extension)
2
[Email protected]:~# python-v
Python 2.7.3
3
[Email protected]:~# apt-get install-y python-psycopg2 python-psycopg2-doc python-psycopg2-dbg
Reading Package Lists ... Done
Building Dependency Tree
Reading state information ... Done
...
Processing triggers for libc-bin ...
Ldconfig deferred processing now taking place
[Email protected]:~#
4
Scripts such as the following:
#--encoding:utf-8--
Import time
Import threading
Import PSYCOPG2
Import Queue
Import datetime
This interface is relevant to its website:
http://initd.org/psycopg/
Class Pgwritertest (threading. Thread):
"" "Initialize" "
def __init__ (SELF,CONNSTR):
Self.conn = Psycopg2.connect (CONNSTR)
Self.cursor = Self.conn.cursor ()
Self.dbnum = 4
Self.connarray = []
Self.cursorarray = []
For I in Range (0,self.dbnum):
#DB Port
Dbidstr = '%02d '% (9900 + i)
if (i = = 0 or i = = 2):
Dstdb = ' host=ip1 user=dbusername password=pwd dbname=dbname port= ' + dbidstr
Else
Dstdb = ' host=ip2 user=dbusername password=pwd dbname=dbname port= ' + dbidstr
print ' Connect ' + dstdb
Dstconn = Psycopg2.connect (DSTDB)
Self.connArray.append (Dstconn)
Dstcursor = Dstconn.cursor ()
Self.cursorArray.append (Dstcursor)
# executes the constructor of the parent class
Threading. Thread.__init__ (self)
"" "Data written to Database" ""
def read (self,t_id):
sql = ' SELECT * from Get_cont (' +str (t_id) + ') '
Self.cursor.execute (SQL)
DataList = []
For row in Self.cursor.fetchall ():
Datalist.append (Row)
return DataList
"" "Data written to Database" ""
def save (Self,t_id,data):
sql = ' SELECT Write_cont ((%s,character (255)%s,%d,now ():: timestamp)) '
params = []
Params.append (t_id)
Params.append (Data[1])
Params.append (Data[2])
Params.append (Data[3])
#print params
#取db_ins_id
db_ins_id = 0
db_ins_id = t_id% self.dbnum
Try
Print str (Datetime.datetime.now ()) + "DB" + str (db_ins_id) + "+ str (t_id) +" before insert "
Insert_sql = ' SELECT Write_cont ((%s,character (255)%s,%d,now ():: timestamp)) '
Self.cursorarray[db_ins_id].execute (Insert_sql,params)
Self.cursorarray[db_ins_id].execute ("COMMIT")
Print str (Datetime.datetime.now ()) + "DB" + str (db_ins_id) + "+ str (t_id) +" inserted "
Return True
Except Exception,ex:
Print ("Save error:%s"% str (ex))
Print ("Save t_id:%s\t"% str (t_id))
Print ("Error cont:%s"% str (params))
#self. Log.write ("error param:%s"% str (params))
#self. Log.write ("error t_id:%s"% data.get (' t_id ', '))
Self.cursorarray[db_ins_id].execute ("ROLLBACK")
Return False
#測试读取
"""
Try
Searchsql = ' Select t_id from Get_cont (' + str (t_id) + ') '
Self.cursorarray[db_ins_id].execute (Searchsql)
data = Self.cursorarray[db_ins_id].fetchone ()
if (data = = None or int (data[0])! = t_id):
print ' Insert error for ' +str (db_ins_id) + Searchsql
Except Exception,searchex:
Print str (searchex) + ' for ' + searchsql
"""
Return True
if __name__ = = "__main__":
Pgtest = Pgwritertest (' host=ip user=dbuser password=pwd dbname=dbname port=9999 ')
start_time = str (Datetime.datetime.now ())
For I in Range (1,10):
t_id = + i;
Print str (Datetime.datetime.now ()) + "getting"
rows = Pgtest.read (t_id)
Print str (Datetime.datetime.now ()) + "gotten"
#print Rows[0]
Pgtest.save (T_id,rows[0])
Print Start_time
Print str (Datetime.datetime.now ())
-----------------
Reprint Please specify the Source:
Blog.csdn.net/beiigang
Ubuntu running Python connection PG, error Importerror:no module named PSYCOPG2