正所谓工欲善其事必先利其器,学习
Python
之前,我们需要先掌握如何安装Python
版本。因为Python
安装途径有很多,加之操作系统的不同,所以安装方式比较多。
1. 使用系统的包管理工具
- 官网 - www.python.org
- 官方自带的
Python
版本都是稳定性大于更新率
- 官方自带的
# Ubuntu系统一般更新比较及时的
[escape@localhost ~]$ sudo apt-get install python3.6
# CentOS一般情况下是找不到对应的Python安装包的
[escape@localhost ~]$ sudo yum install python3.6
- PPA - Personal Package Archives
- 个人用户提供的
Python
版本都是更新率大于稳定性
- 个人用户提供的
[escape@localhost ~]$ sudo apt-get install software-properties-common
[escape@localhost ~]$ sudo add-apt-repository ppa:XXX/ppa
[escape@localhost ~]$ sudo apt-get update
[escape@localhost ~]$ sudo apt-get install python3.7
2. 官网找对应的平台安装包
- 如果我们对
Python
版本和特性没有定制化的需求,一般使用官网对应系统平台的安装包是最佳选择,因为这个安装包是针对于该平台进行过优化和设置的。
3. 通过源代码编译安装
在做运维的时候,我们可以参考
gentoo
的软件编译方式,如其中Python
版本的安装
- 优点
- 普遍适用
- 可定制化
- 及时体验最新版本
- 缺点
- 编译很慢
- 没有对对应平台做过优化和设置
# xz相对于其他压缩格式的压缩率更高,所以文件更小
https://www.python.org/ftp/python/3.7.0/Python-3.7.0rc1.tgz
https://www.python.org/ftp/python/3.6.6/Python-3.6.6rc1.tar.xz
4. Python 多版本共存安装
- 查看当前系统的 Python 版本
[escape@localhost ~]$ which python
/usr/bin/python
[escape@localhost ~]$ python -V
Python 2.7.5
- 在官网找到对应的连接地址
[escape@localhost ~]$ mkdir software
[escape@localhost ~]$ cd software/
[escape@localhost ~]$ wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6rc1.tar.xz
[escape@localhost ~]$ tar -xvf Python-3.6.6rc1.tar.xz
[escape@localhost ~]$ cd Python-3.6.6rc1
- 编译安装
# 收集系统环境信息生成Makefile文件
[escape@localhost ~]$ ./configure
# 根据Makefile文件编译Python程序
[escape@localhost ~]$ make
# 安装Python程序
[escape@localhost ~]$ sudo make install
- 生成链接文件
# 对pip进行连接,之后方便我们安装软件
[escape@localhost ~]$ which pip3
/usr/local/bin/pip3
[escape@localhost ~]$ sudo ln -s /usr/local/bin/pip3 /usr/bin/pip
[escape@localhost ~]$ which pip
/usr/bin/pip
# 我们这里连接Python版本之后,会出现yum无法使用的情况,请查看我之前的博客
[escape@localhost ~]$ sudo mv /usr/bin/python /usr/bin/python.bak
[escape@localhost ~]$ sudo ln -s /usr/local/bin/python3.6 /usr/bin/python
[escape@localhost ~]$ python -V
- 安装额外的软件
# 在安装软件之前,系统说没有SSL的支持,所有需要安装扩展库程序
[escape@localhost ~]$ sudo yum install openssl openssl-devel
# 安装完扩展之后需要,从新编译一下
[escape@localhost ~]$ ./configure && make && sudo make install
[escape@localhost ~]$ sudo pip install ipython
[escape@localhost ~]$ sudo pip install pipenv
5. IPython 的基本用法
- 退出时能够保存历史记录以备未来查询
- 支持
tab
自动补全函数或者命令 - 能够快速获取模块/函数/类/文档/原始代码的信息
- 方便在交互式环境下执行
Shell
命令
[escape@localhost ~]$ ipython
In [1]: 1 + 2
Out[1]: 3
In [2]: print('Hello World!')
Hello World!
In [3]: L = list()
In [4]: L.append(1)
In [5]: L.extend([2, 3])
In [6]: L
Out[6]: [1, 2, 3]
[escape@localhost ~]$ ipython
In [7]: a = 1
In [8]: b = 0
In [9]: a / b
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-3-d8e10abd5ab6> in <module>()
----> 1 a / b
ZeroDivisionError: division by zero
In [10]: %debug
> <ipython-input-3-d8e10abd5ab6>(1)<module>()
----> 1 a / b
ipdb> p a
1
ipdb> p b
0
ipdb>
In [6]: hist
a = 1
b = 0
a / b
%debug
In [1]: a = 1
In [2]: b = 0
In [3]: a / b
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-3-d8e10abd5ab6> in <module>()
----> 1 a / b
ZeroDivisionError: division by zero
In [4]: %debug
> <ipython-input-3-d8e10abd5ab6>(1)<module>()
----> 1 a / b
ipdb> p a
1
ipdb> p b
0
ipdb>l
hist
In [7]: import os
In [8]: os.getcwd()
Out[8]: '/usr/bin'
In [9]: os.getcwd?
Signature: os.getcwd()
Docstring: Return a unicode string representing the current working directory.
Type: builtin_function_or_method
In [10]: os?
Type: module
String form: <module 'os' from '/usr/local/lib/python3.6/os.py'>
File: /usr/local/lib/python3.6/os.py
Docstring:
OS routines for NT or Posix depending on what system we're on.
This exports:
- all functions from posix or nt, e.g. unlink, stat, etc.
- os.path is either posixpath or ntpath
- os.name is either 'posix' or 'nt'
- os.curdir is a string representing the current directory (always '.')
- os.pardir is a string representing the parent directory (always '..')
- os.sep is the (or a most common) pathname separator ('/' or '\\')
- os.extsep is the extension separator (always '.')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)
Programs that import and use 'os' stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
In [15]: ls
anaconda-ks.cfg software/
In [16]: !uptime
13:35:17 up 3:41, 2 users, load average: 0.01, 0.06, 0.05