1、將Python3.6.5安裝包及python36_install.sh放在同一目錄
# ll -thr-rw-r--r--. 1 root root 22M Aug 10 05:09 Python-3.6.5.tgz -rw-r--r--. 1 root root 1.2K Aug 10 20:58 python36_install.sh -rw-r--r--. 1 root root 71K Aug 28 04:50 redis-3.5.3-py2.py3-none-any.whl
安裝腳本內容
# vim python36_install.sh
#!/bin/bash
#===============================================================================
#
# FILE: python36_install.sh
#
# USAGE: install python36 script
#
# DESCRIPTION: To install python36 automatically
#
# BUGS: ---
# NOTES: ---
# AUTHOR: chh-huang
# CREATED: 04/13/2020 08:46:05 PM
# VERSION: 1.0
#=============================================================================== tar -vxf Python-3.6.5.tgz if [[ $? = 0 ]];then echo '' echo -e "\e[1;32mpython3.6 unzip success.\e[0m" cd Python-3.6.5 yum install gcc openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib* -y if [[ $? = 0 ]];then echo -e "\e[1;32myum install success.\e[0m" ./configure --prefix=/usr/local/python3 if [[ $? = 0 ]];then echo -e "\e[1;32mpython configure success.\e[0m" make if [[ $? = 0 ]];then echo -e "\e[1;32mpython make success.\e[0m" make install if [[ $? = 0 ]];then echo -e "\e[1;32mpython make install success.\e[0m" ln -sv /usr/local/python3/bin/python3 /usr/bin/python3 ln -sv /usr/local/python3/bin/pip3 /usr/bin/pip3 echo -e "\e[1;32mpython3.6 install success.\e[0m" python3 else echo -e "\e[1;31mpython3.6 make install failed.\e[0m" exit fi else echo -e "\e[1;31mpython make failed.\e[0m" exit fi else echo -e "\e[1;31mpython configure failed.\e[0m" exit fi else echo -e "\e[1;31m2yum install failed.\e[0m" exit fi else echo '' echo -e "\e[1;31mpython3.6 unzip failed.\e[0m" exit fi
2、執行腳本
sh python36_install.sh
3、會自動安裝好Python和pip,並做成軟鏈接,與現有Python不沖突

4、離線安裝pip包
有的生產系統服務器不允許連接內網,所以要用離線方法安裝
先在能上網服務器下載
pip3 install redis --download /tmp
# 下載后的文件以whl后綴的文件,在/tmp目錄
-rw-r--r-- 1 root root 71K 8月 28 16:30 redis-3.5.3-py2.py3-none-any.whl
將文件拷貝到服務器上安裝,將文件放到/root目錄下
# pip3 install --no-index --find-links="/root" redis
Collecting redis
Installing collected packages: redis
Successfully installed redis-3.5.3
訪問試試
# python3 Python 3.6.5 (default, Aug 28 2020, 17:11:02) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>>
5、離線下載時報錯,pip新離線下載方法
[root@hch tmp 10:46:56]# pip36 install requests --download /tmp/offline_packages Usage: pip36 install [options] <requirement specifier> [package-index-options] ... pip36 install [options] -r <requirements file> [package-index-options] ... pip36 install [options] [-e] <vcs project url> ... pip36 install [options] [-e] <local project path> ... pip36 install [options] <archive url/path> ... no such option: --download # 離線下載requests庫,如果豆瓣源不是https還要添加信任 pip36 download requests --trusted-host pypi.douban.com # 在無外網服務器上安裝, requests為離線包目錄 # pip3 install --no-index --find-links=file:./requests requests Collecting requests Collecting certifi>=2017.4.17 (from requests) Collecting idna<3,>=2.5 (from requests) Collecting urllib3<1.27,>=1.21.1 (from requests) Collecting chardet<5,>=3.0.2 (from requests) Installing collected packages: certifi, idna, urllib3, chardet, requests Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.3
參考
Python pip離線安裝package方法總結(以TensorFlow為例) | 毛帥的博客
https://imshuai.com/python-pip-install-package-offline-tensorflow
轉載請注明出處!!!
