在Python中,安装第三方模块,是通过setuptools这个工具完成的。
Python有两个封装了setuptools的包管理工具:easy_install和pip。
目前官方推荐使用pip。
Linux下python2.7安装pip
首先下载并安装setuptools:
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py --insecure
再到python官网下载pip安装包,解压到某个位置,我这里下载的是8.0.0版本,然后就可以安装了:
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9 --no-check-certificate
tar -xf pip-9.0.1.tar.gz
cd pip-8.0.0
sudo python setup.py install
ln -s /usr/local/python27/bin/pip /usr/bin/pip
zabbix:/root/pip-9.0.1# python
Python 2.7.3 (default, Apr 9 2017, 14:47:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named redis
zabbix:/root/pip-9.0.1# pip install redis
zabbix:/root/pip-9.0.1# python
Python 2.7.3 (default, Apr 9 2017, 14:47:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>>
如果出现urllib2.URLError:<urlopen error unknown url type: https>
python 出现'module' object has no attribute 'HTTPSHandler'错误,是因为你编译python的时候没有把ssl模块编译进去。
yum insatll *ssl*
cd python-2.7.5
./configure --prefix=/usr/local/python27 #此一步务必记住不要覆盖掉centos6.4自带的python26,指定一个新的安装路径
make && make install
重新做连接:
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python
或者:
ela01:/root/install/pip-9.0.1# python setup.py install
Traceback (most recent call last):
File "setup.py", line 6, in <module>
from setuptools import setup, find_packages
ImportError: No module named setuptools
直接安装pip 会报需要setuptools
原因是:安装pip时提示setup.py中使用setuptools中的模块,但是电脑中并没有安装setuptools软件包,所以我们需要先下载并安装 setuptools!
1.先安装
setuptools-34.3.3.zip