本文档目标搭建python和pip,为后续的学习提供支持
本文档主要分三部分:1、python包括pip的安装;2、写第一个python代码;3、介绍pip的使用方法
本文档基于centos8,python3
python安装
[root@LINCB ~]# yum install python3
#安装python,默认自带安装pip(用于安装python第三方库)
[root@LINCB ~]# python3 -V
Python 3.6.8
[root@LINCB ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
#验证python和pip是否安装成功
[root@LINCB ~]# ln -s /etc/alternatives/python3 /usr/bin/python
[root@LINCB ~]# ln -s /etc/alternatives/pip3 /usr/bin/pip
#(非必须)设置python和pip的软链接,配置之后可以直接使用python、pip启动
[root@LINCB ~]# mkdir ~/.pip
[root@LINCB ~]# echo "[global]" >> ~/.pip/pip.conf
[root@LINCB ~]# echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> ~/.pip/pip.conf
#配置清华大学镜像源,用于解决由于网络原因造成的安装缓慢问题,可将路径换成其他国内源
第一个python程序
[root@LINCB ~]# vim hello.py
#新建hello.py并输入如下内容:
#!/usr/bin/python3
print("Hello, World!")
[root@LINCB ~]# python hello.py
Hello, World!
#查看结果,第一个程序完成
pip安装第三方库
这里将介绍两种:1、直接安装;2、下载到本地再安装,适用于无网络或者批量安装。
[root@LINCB ~]# pip install pandas
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip install --user` instead.
Collecting pandas
Downloading
...............
Installing collected packages: numpy, pytz, pandas
Successfully installed numpy-1.18.1 pandas-1.0.1 pytz-2019.3
#直接安装,若出现Successfully则代表成功
[root@LINCB python]# pip install --download='/home/python/' pandas
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip install --user` instead.
DEPRECATION: pip install --download has been deprecated and will be removed in the future. Pip now has a download command that should be used instead.
Collecting pandas
Downloading
......
Successfully downloaded pandas numpy pytz python-dateutil six
#下载第三方库。若出现如上内容,则代表下载成功
[root@LINCB python]# ll
total 30236
-rw-r--r--. 1 root root 20143300 Feb 23 20:22 numpy-1.18.1-cp36-cp36m-manylinux1_x86_64.whl
-rw-r--r--. 1 root root 10061967 Feb 23 20:22 pandas-1.0.1-cp36-cp36m-manylinux1_x86_64.whl
-rw-r--r--. 1 root root 227183 Feb 23 20:22 python_dateutil-2.8.1-py2.py3-none-any.whl
-rw-r--r--. 1 root root 509158 Feb 23 20:22 pytz-2019.3-py2.py3-none-any.whl
-rw-r--r--. 1 root root 10938 Feb 23 20:22 six-1.14.0-py2.py3-none-any.whl
#将会自动下载相关依赖
[root@LINCB python]# pip install --no-index -f file:///home/python/ pandas
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip install --user` instead.
Collecting pandas
.......
Installing collected packages: pandas
Successfully installed pandas-1.0.1
#本地安装成功