查看系统中的python版本,如系统中没有python可以到 python.org 网站下载python,支持linux、windows、macos系统。下文可以看到系统已经安装过了python2.7版本。
[root@linux-01 ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@linux-01 ~]# python -V
Python 2.7.5
安装pip命令
pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。
安装过python后是没有pip命令的,需要安装pip命令在Centos可以用yum来安装,也可以下载源码安装。
python2中安装pip命令需要用到python2-pip.noarch;python3都安装pip3命令需要用到python3-pip.noarch。两个包管理文件不一样。
[root@linux-001 ~]# yum list |grep python2-pip
python2-pip.noarch 8.1.2-14.el7 epel
[root@linux-001 ~]# yum list |grep python3-pip
python3-pip.noarch 9.0.3-8.el7 base
[root@linux-001 ~]# yum install -y python2-pip
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 4.3 kB 00:00:00
* base: mirrors.aliyun.com
* centos-sclo-rh: mirrors.163.com
* centos-sclo-sclo: mirrors.163.com
* epel: mirror.sjtu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Webmin | 2.9 kB 00:00:00
base | 3.6 kB 00:00:00
centos-sclo-rh | 3.0 kB 00:00:00
centos-sclo-sclo | 3.0 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
salt-latest | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
zabbix | 2.9 kB 00:00:00
zabbix-non-supported | 2.9 kB 00:00:00
(1/5): epel/x86_64/group_gz | 95 kB 00:00:05
(2/5): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/5): epel/x86_64/primary_db | 6.9 MB 00:00:00
(4/5): Webmin/primary_db | 147 kB 00:00:09
(5/5): updates/7/x86_64/primary_db | 5.7 MB 00:00:06
正在解决依赖关系
--> 正在检查事务
---> 软件包 python2-pip.noarch.0.8.1.2-14.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================================================================================================
正在安装:
python2-pip noarch 8.1.2-14.el7 epel 1.7 M
事务概要
==================================================================================================================================================================================================================
安装 1 软件包
总下载量:1.7 M
安装大小:7.2 M
Downloading packages:
python2-pip-8.1.2-14.el7.noarch.rpm | 1.7 MB 00:00:06
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : python2-pip-8.1.2-14.el7.noarch 1/1
验证中 : python2-pip-8.1.2-14.el7.noarch 1/1
已安装:
python2-pip.noarch 0:8.1.2-14.el7
完毕!
安装python交互式解释器ipython
IPython是一种基于Python的交互式解释器。相较于本地的Python Shell,IPython提供了更为强大的编辑和交互功能。
在python2中使用pip命令 pip install ipython==1.2.1 即可安装ipython。 在python3中可以使用pip3 install ipthon安装ipython。
[root@linux-001 ~]# pip install ipython
Collecting ipython
Downloading https://files.pythonhosted.org/packages/9c/7b/ca693dd606ccfe12cdc1623dc5e4ca24cbcab5a1ec39f5e5576b72d5cae7/ipython-7.21.0.tar.gz (5.1MB)
100% |████████████████████████████████| 5.2MB 75kB/s
Complete output from command python setup.py egg_info:
IPython 7.17+ supports Python 3.7 and above, following NEP 29.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Python 3.3 and 3.4 were supported up to IPython 6.x.
Python 3.5 was supported with IPython 7.0 to 7.9.
Python 3.6 was supported with IPython up to 7.16.
See IPython `README.rst` file for more information:
https://github.com/ipython/ipython/blob/master/README.rst
Python sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0) detected.
Your pip version is out of date, please install pip >= 9.0.1. pip 8.1.2 detected.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-fHyIYj/ipython/
You are using pip version 8.1.2, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@linux-001 ~]# pip install ipython==1.2.1
Collecting ipython==1.2.1
Downloading https://files.pythonhosted.org/packages/42/a0/204ecd8b4e76f3399b51addbd39980ec5fa90275ff853879e25508030922/ipython-1.2.1.tar.gz (8.7MB)
100% |████████████████████████████████| 8.7MB 48kB/s
Installing collected packages: ipython
Running setup.py install for ipython ... done
Successfully installed ipython-1.2.1
You are using pip version 8.1.2, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@linux-001 ~]# ipython
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: exit
Python的文件类型
root@linux-01 ~]# mkdir mypythonshell
[root@linux-01 ~]# cd mypythonshell/
[root@linux-01 mypythonshell]# vim 1.py
#/usr/bin/python
print 'hello word!'
[root@linux-01 mypythonshell]# python 1.py
hello word!
[root@linux-01 mypythonshell]# vim 2.py
#!/usr/bin/python
# py_compile 模块提供了用来从源文件生成字节码的函数和另一个用于当模块源文件作为脚本被调用时的函数。
import py_compile
py_compile.compile('./1.py')
[root@linux-01 mypythonshell]# ll
总用量 8
-rw-r--r-- 1 root root 38 3月 4 23:33 1.py
-rw-r--r-- 1 root root 66 3月 4 23:35 2.py
[root@linux-01 mypythonshell]# python 2.py
hello word!
[root@linux-01 mypythonshell]# ll
总用量 12
-rw-r--r-- 1 root root 38 3月 4 23:33 1.py
-rw-r--r-- 1 root root 114 3月 4 23:35 1.pyc
-rw-r--r-- 1 root root 66 3月 4 23:35 2.py
# -O 优化生成的字节码,-m 以脚本形式运行库模块
[root@linux-01 mypythonshell]# python -O -m py_compile 1.py
[root@linux-01 mypythonshell]# ll
总用量 16
-rw-r--r-- 1 root root 38 3月 4 23:33 1.py
-rw-r--r-- 1 root root 114 3月 4 23:35 1.pyc
-rw-r--r-- 1 root root 112 3月 4 23:36 1.pyo
-rw-r--r-- 1 root root 66 3月 4 23:35 2.py
[root@linux-01 mypythonshell]# python 1.pyo
hello word!