Ubuntu running Python connection PG, error Importerror:no module named PSYCOPG2

Source: Internet
Author: User

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


Install as follows:
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
The script is as follows:
#--encoding:utf-8--
Import time
Import threading
Import PSYCOPG2
Import Queue
Import datetime




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)

# Run 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 ())

Ubuntu running Python connection PG, error Importerror:no module named PSYCOPG2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.