前言
因Linux下有自帶的python2系列,系統(tǒng)中有一些自帶的工具需要用到2系列的(比如yum),所以還不能進行卸載。
安裝3系列后,2與3并存,但安裝第三方庫,用import導入時會發(fā)現模塊不存在
在Python環(huán)境下,如果想操作MySQL數據庫,難免會調用相應的包,比如mysqldb
使用pip安裝時發(fā)現找不到合適的版本
[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
經查 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
進入python ide環(huán)境,用 import 導入 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ā)現安裝在了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是因為我安裝后設置的軟連接是指向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
若出現下述問題,說明setuptools沒安裝好,可以回看當時安裝時的日志(查報錯原因,看是否缺少依賴包等)
[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)后進行導入測試
[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
>>>
哈哈哈 當你看到“>>>”時就表明 ok啦
接著繼續(xù)測試一下數據庫連接(用戶名和密碼根據實際情況填寫)
>>> 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()
>>>