Python3.6.5和pip安装脚本以及离线安装pip包【原创】

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
pip36 download requests
# 在无外网服务器上安装, 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

 

 

 

转载请注明出处!!!

posted @ 2020-08-28 17:26  paul_hch  阅读(1218)  评论(0编辑  收藏  举报