Python 安装requests和MySQLdb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 安装requests和MySQLdb相关的知识,希望对你有一定的参考价值。

Python 安装requests和MySQLdb

2017年10月02日

0.系统版本

0.1 查看系统版本

1 [[email protected] ~]# uname -a
2 Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

1.安装requests

1.1 软件下载

1 [[email protected] ~]# wget 2 https://pypi.python.org/packages/b0/e1/eab4fc3752e3d240468a8c0b284607899d2fbfb236a56b7377a329aa8d09/requests-2.18.4.tar.gz#md5=081412b2ef79bdc48229891af13f4d82

1.2 解压

1 [[email protected] ~]# tar xf requests-2.18.4.tar.gz 

1.3安装requests

1 [[email protected] ~]# cd requests-2.18.4
2 [[email protected] requests-2.18.4]# python setup.py install

1.4 安装报错

1 [[email protected] supervisor-3.0]# python setup.py install  
2 Traceback (most recent call last):  
3   File "setup.py", line 31, in <module>  
4     from setuptools import setup, find_packages  
5 ImportError: No module named setuptools  

原因是没有找到setuptools包,再次安装这个包

下载setuptools :

1 [[email protected] requests-2.18.4]#wget 2 https://pypi.python.org/packages/a4/c8/9a7a47f683d54d83f648d37c3e180317f80dc126a304c45dc6663246233a/setuptools-36.5.0.zip#md5=704f500dd55f4bd0be905444f3ba892c

利用unzip进行解压

1 [[email protected] requests-2.18.4]# unzip setuptools-36.5.0.zip 

如果提示没有找到unzip命令,则利用yum安装即可

1 [[email protected] requests-2.18.4]# yum install zip unzip 

解压完成后即可进行安装

1 [[email protected] requests-2.18.4]# cd setuptools-36.5.0
2 [[email protected] setuptools-36.5.0]# python setup.py install

安装完成后即可进行安装requests

1 [[email protected] setuptools-36.5.0]# cd ../
2 [[email protected] requests-2.18.4]# python setup.py install

 

安装完成检验,进入python交互式界面进行导入包即可

1 [[email protected] requests-2.18.4]# python
2 Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import requests
6 >>> 

 

2.安装requests

2.1 软件下载

1 [[email protected] ~]# wget 2 https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c

2.2 解压并且安装

1 [[email protected] ~]# unzip MySQL-python-1.2.5.zip 
2 [[email protected] ~]# cd MySQL-python-1.2.5
3 [[email protected] MySQL-python-1.2.5]# python setup.py install

2.3 安装报错

1 sh: mysql_config: not found
2 Traceback (most recent call last):
3   File "setup.py", line 15, in <module>
4     metadata, options = get_config()
5   File "/home/zhxia/apps/source/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
6     libs = mysql_config("libs_r")
7   File "/home/zhxia/apps/source/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
8     raise EnvironmentError("%s not found" % (mysql_config.path,))
9 EnvironmentError: mysql_config not found

安装libmysqlclient-dev

1 [[email protected] MySQL-python-1.2.5]# yum install libmysqlclient-dev

安装报错,并没有发现libmysqlclient-dev

1 [[email protected] requests-2.18.4]# yum install libmysqlclient-dev
2 Loaded plugins: fastestmirror
3 Loading mirror speeds from cached hostfile
4  * base: mirrors.sohu.com
5  * extras: mirrors.sohu.com
6  * updates: mirrors.sohu.com
7 No package libmysqlclient-dev available.
8 Error: Nothing to do
9 [[email protected] requests-2.18.4]# 

 

之所以会出现如下原因是系统没有找到mysql的依赖包,那么我们可以自己安装依赖,这儿依赖可以为mysql 也可以为mariadb

1 [[email protected] MySQL-python-1.2.5]# yum list all | grep mariadb | grep dev
2 mariadb-devel.x86_64                        1:5.5.56-2.el7             @base    
3 mariadb-embedded-devel.x86_64               1:5.5.56-2.el7             @base    
4 mariadb-devel.i686                          1:5.5.56-2.el7             base     
5 mariadb-embedded-devel.i686                 1:5.5.56-2.el7             base     
6 [[email protected] MySQL-python-1.2.5]# 

 

安装mariadbdev

1 [[email protected] MySQL-python-1.2.5]# yum install mariadb-devel mariadb-embedded-devel

再次安装MySQL-python-1.2.5

[[email protected] MySQL-python-1.2.5]# python setup.py install

再次报错

 1 [[email protected] MySQL-python-1.2.5]# python setup.py install
 2 running install
 3 running bdist_egg
 4 running egg_info
 5 writing MySQL_python.egg-info/PKG-INFO
 6 writing top-level names to MySQL_python.egg-info/top_level.txt
 7 writing dependency_links to MySQL_python.egg-info/dependency_links.txt
 8 reading manifest file MySQL_python.egg-info/SOURCES.txt
 9 reading manifest template MANIFEST.in
10 writing manifest file MySQL_python.egg-info/SOURCES.txt
11 installing library code to build/bdist.linux-x86_64/egg
12 running install_lib
13 running build_py
14 copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
15 running build_ext
16 building _mysql extension
17 gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,5,final,1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o
18 _mysql.c:29:20: fatal error: Python.h: No such file or directory
19  #include "Python.h"
20                     ^
21 compilation terminated.
22 error: command gcc failed with exit status 1

再次安装python-devel

1 [[email protected] MySQL-python-1.2.5]# yum install python-devel 

再次尝试

1 [[email protected] MySQL-python-1.2.5]# python setup.py install

检测是否安装OK

1 [[email protected] MySQL-python-1.2.5]# python 
2 Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import MySQLdb
6 /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib64/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/_mysql.pyc, but /root/python-script/MySQL-python-1.2.5 is being added to sys.path
7 >>> 

安装requestsMySQLdb到此结束

关于python包的下载:https://pypi.python.org/pypi可以索搜并且下载包

 

以上是关于Python 安装requests和MySQLdb的主要内容,如果未能解决你的问题,请参考以下文章

如何用python的pip安装mysqldb

在linux上用新版python安装mysqldb

在雪豹上安装mysqldb

Eclipse下Python的MySQLdb的安装以及相关问题

MySQLdb flask-MySQLdb MySQL-python 安装失败

linux python 安装MySQLdb