(注意:windows下面在py的安装根目录下的Script目录下自带有,而Linux)
1. 安装必要的准备包
a、安装过程将用到gcc,方便起见,安装“Development Tools”:
yum grouplist
yum groupinstall -y "Development tools"
b、另外,Python安装中需要的一些依赖包:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
2. 下载安装Python2.7
目前Python2.7的我装的版本是2.7.9。可以在下面的网站查询:
https://www.python.org/ftp/python/
2.7.11的下载链接是:
https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
//或者用root用户安装(普通用户最好最如下):
sudo tar -zxvf /home/hadoop/Python-2.7.9.tgz -C /usr/local/
cd /usr/local/Python-2.7.9/
sudo chown -R hadoop:hadoop /usr/local/Python-2.7.9/
sudo ./configure --prefix=/usr/local/Python-2.7.9 (将在该目录产生/bin)
sudo make && make install
3、安装完成后,通过运行python,可以看到版本
[root@hadoop007 Python-2.7.9]# python
Python 2.7.9 (default, Jul 2 2017, 04:42:14)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version
'2.7.9 (default, Jul 2 2017, 04:42:14) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]'
[hadoop@hadoop007 ~]$ python -V
Python 2.7.9
4.pip和easy_install的介绍:
a. easy_install和pip都是用来下载安装Python一个公共资源库PyPI
的相关资源包的,pip是easy_install的改进版,提供更好的提示信
息,删除package等功能。老版本的python中只有easy_install,没有pip。
b. easy_install 打包和发布Python 包
c. pip 是包管理
5.安装pip
pip是python的安装工具,很多python的常用工具,都可以通过pip进行安装。
要安装pip,首先要安装setuptools。下面的链接可以得到相关信息,装版本是21.0.0:
https://pypi.python.org/pypi/setuptools
下载链接:
https://pypi.python.org/packages/ff/d4/209f4939c49e31f5524fa0027bf1c8ec3107abaf7c61fdaad704a648c281/setuptools-21.0.0.tar.gz#md5=81964fdb89534118707742e6d1a1ddb4
同样的,进行安装:
tar -zxvf /home/hadoop/setuptools-21.0.0.tar.gz -C /usr/local/
cd /usr/local/setuptools-21.0.0
python setup.py install
安装完成后,下载pip。其信息在如下网站:
https://pypi.python.org/pypi/pip
版本是8.1.1,下载链接:
https://pypi.python.org/packages/41/27/9a8d24e1b55bd8c85e4d022da2922cb206f183e2d18fee4e320c9547e751/pip-8.1.1.tar.gz#md5=6b86f11841e89c8241d689956ba99ed7
同样的,进行安装
tar -zvxf /home/hadoop/pip-8.1.1.tar.gz -C /usr/local/
cd /usr/local/pip-8.1.1/
python setup.py install
安装完成后,运行pip:
测试:
[root@hadoop007 pip-8.1.1]# pip
Usage:
pip
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and
user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3
times.
-V, --version Show version and exit.
-q, --quiet Give less output.
--log
--proxy
--retries
5 times).
--timeout
--exists-action
(w)ipe, (b)ackup.
--trusted-host
any HTTPS.
--cert
--client-cert
private key and the certificate in PEM format.
--cache-dir
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version
of pip is available for download. Implied with --no-index.
查看所有pip安装的程序:
[root@hadoop007 pip-8.1.1]# pip freeze
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
安装一个网络爬虫所需requests:
[root@hadoop007 pip-8.1.1]# pip install requests
Collecting requests
Downloading requests-2.18.1-py2.py3-none-any.whl (88kB)
100% |████████████████████████████████| 92kB 410kB/s
Collecting idna<2.6,>=2.5 (from requests)
Downloading idna-2.5-py2.py3-none-any.whl (55kB)
100% |████████████████████████████████| 61kB 3.3MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading certifi-2017.4.17-py2.py3-none-any.whl (375kB)
95% |██████████████████████████████▌| 358kB 433kB/s eta 0:00:0
98% |███████████████████████████████▍| 368kB 437kB/s eta 0:00:
100% |████████████████████████████████| 378kB 363kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
99% |████████████████████████████████| 133kB 461kB/s eta 0:00:
100% |████████████████████████████████| 143kB 415kB/s
Collecting urllib3<1.22,>=1.21.1 (from requests)
Downloading urllib3-1.21.1-py2.py3-none-any.whl (131kB)
100% |████████████████████████████████| 133kB 327kB/s
Installing collected packages: idna, certifi, chardet, urllib3, requests
Successfully installed certifi-2017.4.17 chardet-3.0.4 idna-2.5 requests-2.18.1 urllib3-1.21.1
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
更新pip的版本为最新版本pip-9.0.1:
[root@hadoop007 pip-8.1.1]# pip install --upgrade pip
Collecting pip
Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
96% |██████████████████████████████▉| 1.2MB 123kB/s eta 0:00:0
97% |███████████████████████████████| 1.2MB 109kB/s eta 0:00:0
97% |███████████████████████████████▍| 1.2MB 98kB/s eta 0:00:0
98% |███████████████████████████████▋| 1.2MB 113kB/s eta 0:00:
99% |███████████████████████████████▉| 1.2MB 133kB/s eta 0:00:
100% |████████████████████████████████| 1.3MB 116kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-9.0.1
(注意:windows下面在py的安装根目录下的Script目录下自带有,而Linux)
其实我们安装好setup_tool工具就已经安装好了easy_install工具包。如果发现没有安装好,即可以使用下面的命令:
[root@hadoop007 ~]#python ez_setup.py
[root@hadoop007 ~]# easy_install --help #测试是否安装好
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
--no-user-cfg ignore pydistutils.cfg in your home directory
Options for 'easy_install' command:
--prefix installation prefix
--zip-ok (-z) install package as a zipfile
--multi-version (-m) make apps have to require() a version
--upgrade (-U) force upgrade (searches PyPI for latest versions)
--install-dir (-d) install package to DIR
--script-dir (-s) install scripts to DIR
--exclude-scripts (-x) Don't install scripts
--always-copy (-a) Copy all needed packages to install dir
--index-url (-i) base URL of Python Package Index
--find-links (-f) additional URL(s) to search for packages
--build-directory (-b) download/extract/build in DIR; keep the results
--optimize (-O) also compile with optimization: -O1 for "python -
O", -O2 for "python -OO", and -O0 to disable
[default: -O0]
--record filename in which to record list of installed
files
--always-unzip (-Z) don't install as a zipfile, no matter what
--site-dirs (-S) list of directories where .pth files work
--editable (-e) Install specified packages in editable form
--no-deps (-N) don't install dependencies
--allow-hosts (-H) pattern(s) that hostnames must match
--local-snapshots-ok (-l) allow building eggs from local checkouts
--version print version information and exit
--no-find-links Don't load find-links defined in packages being
installed
--user install in user site-package
'/root/.local/lib/python2.7/site-packages'
usage: easy_install [options] requirement_or_url ...
or: easy_install --help
测试安装:
[root@hadoop007 ~]# easy_install bs4
Searching for bs4
Reading https://pypi.python.org/simple/bs4/
Best match: bs4 0.0.1
Downloading https://pypi.python.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz#md5=fe7e51587ac3b174608f3c4f8bd893ac
Processing bs4-0.0.1.tar.gz
Writing /tmp/easy_install-lJJtAp/bs4-0.0.1/setup.cfg
Running bs4-0.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-lJJtAp/bs4-0.0.1/egg-dist-tmp-wgH9Ff
warning: install_lib: 'build/lib' does not exist -- no Python modules to install
zip_safe flag not set; analyzing archive contents...
Moving bs4-0.0.1-py2.7.egg to /usr/local/lib/python2.7/site-packages
Adding bs4 0.0.1 to easy-install.pth file
Installed /usr/local/lib/python2.7/site-packages/bs4-0.0.1-py2.7.egg
Processing dependencies for bs4
Searching for beautifulsoup4
Reading https://pypi.python.org/simple/beautifulsoup4/
Best match: beautifulsoup4 4.6.0
Downloading https://pypi.python.org/packages/fa/8d/1d14391fdaed5abada4e0f63543fef49b8331a34ca60c88bd521bcf7f782/beautifulsoup4-4.6.0.tar.gz#md5=c17714d0f91a23b708a592cb3c697728
Processing beautifulsoup4-4.6.0.tar.gz
Writing /tmp/easy_install-cRsDNj/beautifulsoup4-4.6.0/setup.cfg
Running beautifulsoup4-4.6.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-cRsDNj/beautifulsoup4-4.6.0/egg-dist-tmp-UCsRBa
zip_safe flag not set; analyzing archive contents...
Moving beautifulsoup4-4.6.0-py2.7.egg to /usr/local/lib/python2.7/site-packages
Adding beautifulsoup4 4.6.0 to easy-install.pth file
Installed /usr/local/lib/python2.7/site-packages/beautifulsoup4-4.6.0-py2.7.egg
Finished processing dependencies for bs4
[root@hadoop007 ~]#
同一台服务器,可以有多个相同的python运行环境:如开发、测试、生产
virtualenv
active [环境目录名]激活环境
deactive [环境目录名]退出环境
安装:
pip install virtualenv
创建开发、生产、测试环境:
virtualenv /home/hadoop/dev
virtualenv /home/hadoop/test
virtualenv /home/hadoop/pro
切换dev环境:
source /home/hadoop/dev/bin/activate (注意看前面多咯一个环境名)
退出dev环境:
deactivate
查看dev环境的安装包:
pip freeze (会发现没有)
在dev中安装requests:
pip install requests
一个最简单的爬虫:
#新建/home/hadoop/pythoncode/demo.py
#!/usr/local/bin/python
#coding:utf8
import requests as req
response=req.get("http://www.baidu.com")
print response.status_code
执行该文件:
python ./pythoncode/demo.py