Python3常用模块的安装

1.mysql驱动:mysql-connector-python

1.安装
 $ pip3 install mysql-connector-python --allow-external mysql-connector-python
2.引入
  1 #!/usr/bin/env python3
  2 import mysql.connector
  3 
  4 conn=mysql.connector.connect(user='root',password='password',database='test')
  5 cursor=conn.cursor()
  6 cursor.execute('create table user(id varchar(20) primary key,name varchar(2))')
  7 cursor.execute('insert user (id,name) values(%s,%s)',['1','huangbiao'])
  8 print(cursor.rowcount)
  9 conn.commit()
 10 cursor.close()
 11 cursor=conn.cursor()
 12 cursor.execute('select * from user where id=%s',['1'])
 13 values=cursor.fetchall()
 14 print(values)
 15 cursor.close()
 16 conn.close()

2.ORM:sqlalchemy

1.安装
$ pip3 install sqlalchemy
2.引入
  1 #!/usr/bin/env python3
  2 from sqlalchemy import Column,String,create_engine,ForeignKey
  3 from sqlalchemy.orm import sessionmaker,relationship
  4 from sqlalchemy.ext.declarative import declarative_base
  5 """
  6 Base=declarative_base()
  7 #定义User表的ORM
  8 class User(Base):
  9         __tablename__='user'
 10         id =Column(String(20),primary_key=True)
 11         name=Column(String(20))
 12 
 13 engine=create_engine('mysql+mysqlconnector://root:526114@localhost:3306/test')
 14 DBSession=sessionmaker(bind=engine)
 15 
 16 #插入操作
 17 session=DBSession()
 18 new_user=User(id='1',name='Huangbiao')
 19 session.add(new_user)
 20 session.commit()
 21 session.close()
 22 #查询操作
 23 session=DBSession()
 24 user=session.query(User).filter(User.id=='1').one()
 25 print('type:',type(user))
 26 print('name:',user.name)
 27 session.close()
 28 
 29 """
 30 #一对多的关系,一个user对应多本book
 31 Base=declarative_base()
 32 class User(Base):
 33         __tablename__='user'
 34         id=Column(String(20),primary_key=True)
 35         name=Column(String(20))
 36         books=relationship("Book")
 37 class Book(Base):
 38         __tablename__='book'
 39         id=Column(String(20),primary_key=True)
 40         name=Column(String(20))
 41         user_id=Column(String(20),ForeignKey('user.id'))
 42 engine=create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
 43 DBSession=sessionmaker(bind=engine)
 44 session=DBSession();
 45 books=session.query(User).filter(User.id=='1');
 46 for data in books:
 47         print(data.id)
 48         print(data.name)
 49         for book in data.books:
 50                 print('book id:',book.id)
 51                 print('book name:',book.name)
 52 session.close()

3.web框架flask

1.安装
 $ pip3.5 install flask
2.引入
  1 #!/usr/bin/env python3
  2 from flask import Flask
  3 from flask import request
  4 
  5 app=Flask(__name__)
  6 
  7 @app.route('/',methods=['POST','GET'])
  8 def home():
  9         return '<h1>Home is here</h1>'
 10 
 11 @app.route('/signin',methods=['GET'])
 12 def signin_get():
 13         return '''<form action='/signin' method='post'>
 14         <p>Name:<input type='text' name='user_name'></p>
 15         <p>Password:<input type="password" name="password"></p>
 16         <p><button type="submit">Sign In</button></p>
 17         </form>
 18         '''
 19 @app.route('/signin',methods=['POST'])
 20 def sign_post():
 21         if request.form['user_name'].upper()=='ADMIN' and request.form['password']=='admin':
 22                 return '<h1>Login successfully</h1>'
 23         else:
 24                 return '<h1>Wrong name or password</h1>'
 25 if __name__=='__main__':
 26         app.run()

4.aiohttp(基于asyncio实现的HTTP框架)

1.安装
$ pip3 install aiohttp
2.引入
  1 #!/usr/bin/env python3
  2 #asyncio实现TCP,UDP,SSL等协议,aiohttp则是基于asyncio实现的HTTP框架
  3 #使用aiohttp需要先安装
  4 
  5 
  6 #用aiohttp编写一个http服务器
  7 
  8 import asyncio
  9 from aiohttp import web
 10 async def index(request):
 11         await asyncio.sleep(0.5)
 12         return web.Response(body=b'<h1>Index</h1>')
 13 
 14 async def yes(request):
 15         await asyncio.sleep(0.5)
 16         text='<h1>yes,%s!</h1>'%request.match_info['name']
 17         return web.Response(body=text.encode('utf-8'))
 18 
 19 async def init(loop):
 20         app=web.Application(loop=loop)
 21         app.router.add_route('GET','/',index)
 22         app.router.add_route('GET','/yes/{name}',yes)
 23         srv=await loop.create_server(app.make_handler(),'121.42.169.254',8000)
 24         print('Server started at http://121.42.169.254:80...')
 25         return srv
 26 if __name__=='__main__':
 27         loop=asyncio.get_event_loop()
 28         loop.run_until_complete(init(loop))
 29         loop.run_forever()
 30 

5.tkinter(python3.4)

1.安装
apt-get install libc6 
apt-get install libtcl8.6 
apt-get install python3-tk-dbg
apt-get install tix

6.mysqlclient(another python mysql connector)

1.installation
$ pip3.5 install mysqlclient
base usage:
import MySQLdb
#conn=MySQLdb.connect(host='localhost',user='root',passwd='mypassword',db='test') 
conn=MySQLdb.connect('localhost','root','mypassword','test')
cursor=conn.cursor()
cursor.execute('show tables')
print(cursor.fetchall())
cursor.close()
conn.close()

未完待续..................

posted on 2015-12-18 12:48  yeah,youcandoit  阅读(1053)  评论(0编辑  收藏  举报

导航