前言
因Linux下有自帶的python2系列缅阳,系統(tǒng)中有一些自帶的工具需要用到2系列的(比如yum),所以還不能進(jìn)行卸載景描。
安裝3系列后十办,2與3并存秀撇,但安裝第三方庫,用import導(dǎo)入時會發(fā)現(xiàn)模塊不存在
在Python環(huán)境下向族,如果想操作MySQL數(shù)據(jù)庫呵燕,難免會調(diào)用相應(yīng)的包,比如mysqldb
使用pip安裝時發(fā)現(xiàn)找不到合適的版本
[root@izwz94jtz9hbdq165vpxpxz ~]# pip install mysqldb
Collecting mysqldb
Could not find a version that satisfies the requirement mysqldb (from versions: )
No matching distribution found for mysqldb
經(jīng)查 mysqldb 支持到 python3.4件相,3.5以上版本需要使用 pymysql
[root@izwz94jtz9hbdq165vpxpxz ~]# pip install pymysql
Collecting pymysql
Downloading http://mirrors.aliyun.com/pypi/packages/c6/42/c54c280d8418039bd2f61284f99cb6d9e0eae80383fc72ceb6eac67855fe/PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
100% |████████████████████████████████| 81kB 1.1MB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11
進(jìn)入python ide環(huán)境再扭,用 import 導(dǎo)入 pymysql,提示沒有此模塊
[root@izwz94jtz9hbdq165vpxpxz ~]# python
Python 3.5.0 (default, Jun 11 2017, 00:13:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pymysql' ##沒有此模塊
查找pymysql的路徑夜矗,發(fā)現(xiàn)安裝在了2系列的site-packages泛范,所以使用python3會找不到
[root@izwz94jtz9hbdq165vpxpxz ~]# find / -name pymysql
/usr/lib64/python2.7/site-packages/pymysql
想要把第三方庫安裝到3系列下,怎么辦呢侯养?
一、安裝setuptools
1澄干、wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
2逛揩、tar -zxvf setuptools-19.6.tar.gz
3、cd setuptools-19.6
4麸俘、python setup.py build
5辩稽、python setup.py install
使用第5步安裝時報錯:
RuntimeError: Compression requires the (missing) zlib module
解決辦法:
需要安裝zlib-devel包,yum install zlib-devel
注意事項:此處用python是因為我安裝后設(shè)置的軟連接是指向python从媚,如果指向python3的話逞泄,第4、5步也請改成python3
小伙伴們也可以通過官方模塊庫來下載
二拜效、安裝pip
1喷众、wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
2、tar -zxvf pip-8.0.2.tar.gz
3紧憾、cd pip-8.0.2
4到千、python setup.py build
5、python setup.py install
若出現(xiàn)下述問題赴穗,說明setuptools沒安裝好憔四,可以回看當(dāng)時安裝時的日志(查報錯原因,看是否缺少依賴包等)
[root@izwz94jtz9hbdq165vpxpxz pip-8.0.2]# python3 setup.py build
Traceback (most recent call last):
File "setup.py", line 6, in <module>
from setuptools import setup, find_packages
ImportError: No module named 'setuptools'
至此 pip3 安裝完成般眉,接下來安裝第三方庫(pymysql)后進(jìn)行導(dǎo)入測試
[root@izwz94jtz9hbdq165vpxpxz ~]# pip3 install pymysql
Collecting pymysql
Downloading http://mirrors.aliyun.com/pypi/packages/c6/42/c54c280d8418039bd2f61284f99cb6d9e0eae80383fc72ceb6eac67855fe/PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
100% |████████████████████████████████| 81kB 588kB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11
測試是否真正的給python3裝上了這個模塊(而不是裝在python2上了呢)
[root@izwz94jtz9hbdq165vpxpxz ~]# python
Python 3.5.0 (default, Aug 15 2017, 23:19:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>>
哈哈哈 當(dāng)你看到“>>>”時就表明 ok啦
接著繼續(xù)測試一下數(shù)據(jù)庫連接(用戶名和密碼根據(jù)實際情況填寫)
>>> conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='',db='zyptest')
>>> cur = conn.cursor()
>>> sql1 = 'select id,no from student'
>>> cur.execute(sql1)
4
>>> rows = cur.fetchall()
>>> for data in rows:
... print(data)
...
(1, 'A1')
(2, 'A2')
(3, 'A3')
(4, 'A4')
>>>cur.close()
>>>conn.close()
>>>