安装 Python

[root@localhost ~]# mkdir /usr/local/python3 

创建下载目录

[root@localhost ~]# cd /usr/local/python3 

进入目录

[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz 

下载镜像文件

[root@localhost ~]# tar -zxvf Python-3.6.3.tgz  

解压文件

[root@localhost ~]# cd Python-3.6.3/ 

进入文件目录

[root@localhost ~]# ./configure --prefix=/usr/local/python3Dir 

编译文件,指定 python 的安装目录

提前安装以下依赖库,可减少以下内容中的可能出现的报错

[root@localhost ~]# yum install gcc
[root@localhost ~]# yum install zlib-devel
[root@localhost ~]# yum install openssl-devel

如果遇到以下报错

checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/Python-3.6.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

需要执行安装以下依赖库

[root@localhost ~]# yum install gcc
[root@localhost ~]# yum install zlib-devel

如果执行以上安装报错

-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

修改以下配置

[root@localhost ~]# vi /usr/bin/yum

将首行的 #!/usr/bin/python 更改为 #!/usr/bin/python2.6
这是由于系统 python 版本不一致导致,修改后的 python2.6 需要先验证本地版本,再改为实际版本,命令为 python -V,完成后保存退出

[root@localhost ~]# ./configure --prefix=/usr/local/python3Dir 

重新编译

[root@localhost ~]# make && make install 

执行安装
执行以上安装如果报错

Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS 

需要安装以下依赖库

[root@localhost ~]# yum install openssl-devel
[root@localhost ~]# make && make install 

再次执行安装
提示以下即表示安装成功!

.....
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0 

安装成功后验证版本

[root@localhost ~]# python -V
Python 2.6.6 

以上表示还是 2.6.6 版本,centos 自带的 python 版本

此时需要将 python 版本链接到新版本的文件目录

[root@localhost ~]# mv /usr/bin/python /usr/bin/python.bak 
[root@localhost ~]# ln -s /usr/local/python3Dir/bin/python3.5  /usr/bin/python 

再次验证版本

[root@localhost ~]# python -V 
Python 3.5.6 

[root@localhost ~]# python --version 
Python 3.5.6 

完美,成功!

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

安装 pip

[root@localhost ~]# wget https://bootstrap.pypa.io/get-pip.py 
--2019-01-24 21:10:29--  https://bootstrap.pypa.io/get-pip.py
Resolving bootstrap.pypa.io... 151.101.40.175, 2a04:4e42:a::175
Connecting to bootstrap.pypa.io|151.101.40.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1700321 (1.6M) [text/x-python]
Saving to: “get-pip.py”

100%[=================================================================>] 1,700,321    276K/s   in 7.9s    

2019-01-24 21:10:39 (210 KB/s) - “get-pip.py” saved [1700321/1700321]
[root@localhost ~]# python get-pip.py
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting pip
  Downloading http://mirrors.tencentyun.com/pypi/packages/46/dc/7fd5df840efb3e56c8b4f768793a237ec4ee59891959d6a215d63f727023/pip-19.0.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 70.8MB/s 
Collecting wheel
  Downloading http://mirrors.tencentyun.com/pypi/packages/ff/47/1dfa4795e24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-none-any.whl
Installing collected packages: pip, wheel
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
  The script wheel is installed in '/usr/local/python3Dir/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-19.0.1 wheel-0.32.3

如果无法使用 pip 命令

[root@localhost ~]# pip -V 
-bash: pip: command not found 

查看 pip 的安装目录是否在 PATH 中,如果没有,在~/.bash_profile 中添加 export PATH=$PATH:/usr/local/bin(假设 pip 的安装目录为/usr/local/bin)然后 source ~/.bash_profile 使之生效

配置 pip 的 $PATH 环境变量

[root@localhost ~]# vi  ~/.bash_profile 

配置文件
在该文件内添加 export PATH=$PATH:/usr/local/python3Dir/bin 配置

[root@localhost ~]# source ~/.bash_profile 

使该文件生效

[root@localhost ~]# pip -V 
pip 19.0.1 from /usr/local/python3Dir/lib/python3.5/site-packages/pip (python 3.5) 

完美,成功!

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

pip 常用命令

pip --help #获取帮助
pip install -U pip #升级 pip
pip install SomePackage #安装包
pip uninstall SomePackage #卸载包
pip install -U SomePackage #升级指定的包
pip search SomePackage #搜索包
pip show -f SomePackage #查看指定包的详细信息
pip freeze or pip list #列出已安装的包
pip list -o #查看可升级的包
easy_install 用法

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Python / pip / PyPI 三者之间的关系

简单理解,Python 一个编程语言;PyPI 是一个 Python 官方的第三方库的仓库;pip 是一个 Python 包管理工具。

PyPI(Python Package Index)是 Python 官方的第三方库的仓库,所有人都可以下载第三方库或上传自己开发的库到 PyPI,PyPI 推荐使用 pip 包管理器来下载第三方库。
pip(Python Install Packages)是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。

pip 用法
1) 安装一个包

[root@localhost ~]$ pip install 
[root@localhost ~]$ pip install ==

2) 升级一个包 (如果不提供 version 号,升级到最新版本)

[root@localhost ~]$ pip install --upgrade >= 

3)删除一个包

[root@localhost ~]$ pip uninstall  

easy_install 是由 PEAK(Python Enterprise Application Kit)开发的 setuptools 包里带的一个命令,所以使用 easy_install 实际上是在调用 setuptools 来完成安装模块的工作。

1)安装一个包

[root@localhost ~]$ easy_install  
[root@localhost ~]$ easy_install "==" 

2) 升级一个包

[root@localhost ~]$ easy_install -U ">=" 

easy_install 和 pip 都是用来下载安装 Python 一个公共资源库 PyPI 的相关资源包的,pip 是 easy_install 的改进版,提供更好的提示信息,删除 package 等功能。老版本的 python 中只有 easy_install,没有 pip。
easy_install 可执行打包和发布 Python 包;pip 是包管理。


友情提示:本站所有文章,如无特殊说明或标注,均为何星星原创发布。与此同时,趋于近年来本站的文章内容频繁被他站盗用与机器采集,现已全局禁用网站文字内容操作,了解详情或转载文章请 点此 继续!
分类: 系统运维

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注