如何安装Python第三方模块
Python官方为我们提供了第三方库,那么如何安装这些库呢?
安装第三方库有两种方式:
-
第一种就是使用python自带的仓库pip进安装
-
第二种就是使用源码进行安装
PIP方式安装
首先用yum安装python-pip
软件包
1
|
[root@ansheng ~]
# yum install python-pip
|
安装完成之后可以使用pip -V
查看安装版本
1
2
|
[root@ansheng ~]
# pip -V
pip
7.1
.
0
from
/
usr
/
lib
/
python2.
6
/
site
-
packages (python
2.6
)
|
这次就以requests
模块为例把,先查看当前系统有没有安装requests
模块
1
2
3
4
5
6
7
8
9
10
|
[root@ansheng ~]
# python
Python
2.6
.
6
(r266:
84292
, Jul
23
2015
,
15
:
22
:
56
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
11
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
# 如果没有安装在导入的时候就会报错
>>>
import
requests
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ImportError: No module named requests
>>> exit()
|
用pip
的方式安装requests
模块
1
|
[root@ansheng ~]
# pip install requests
|
安装完成之后进入python
解释器导入requests
模块,看看能不能导入成功
1
2
3
4
5
|
[root@ansheng ~]
# python
Python
2.6
.
6
(r266:
84292
, Jul
23
2015
,
15
:
22
:
56
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
11
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
>>>
import
requests
|
安装成功。
卸载执行pip uninstall
加模块名
[root@ansheng ~]# pip uninstall requests
源码包方式安装
下载模块requests
的源码包
1
2
3
4
5
6
7
|
[root@ansheng ~]
# git clone git://github.com/kennethreitz/requests.git
Initialized empty Git repository
in
/
root
/
requests
/
.git
/
remote: Counting objects:
17546
, done.
remote: Compressing objects:
100
%
(
2
/
2
), done.
remote: Total
17546
(delta
0
), reused
0
(delta
0
), pack
-
reused
17544
Receiving objects:
100
%
(
17546
/
17546
),
5.04
MiB |
46
KiB
/
s, done.
Resolving deltas:
100
%
(
11232
/
11232
), done.
|
查看下载下来的文件
1
2
3
4
|
[root@ansheng ~]
# cd requests/
[root@ansheng requests]
# ls
AUTHORS.rst docs HISTORY.rst Makefile NOTICE requests requirements.txt tests
CONTRIBUTING.md ext LICENSE MANIFEST.
in
README.rst requirements
-
to
-
freeze.txt setup.py
|
执行python setup.py install
进行编译安装
1
|
[root@ansheng requests]
# python setup.py install
|
验证是否安装成功
1
2
3
4
5
|
[root@ansheng requests]
# python
Python
2.6
.
6
(r266:
84292
, Jul
23
2015
,
15
:
22
:
56
)
[GCC
4.4
.
7
20120313
(Red Hat
4.4
.
7
-
11
)] on linux2
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
>>>
import
requests
|
安装成功,以上就是Python
第三方模块的两种安装方式,愿能够为你提供帮助。
本文转自 Edenwy 51CTO博客,原文链接:http://blog.51cto.com/edeny/1925133,如需转载请自行联系原作者