python第三方库的离线安装与自动安装脚本(以flask为例 Ubuntu18.04系统)

文章介绍了Python中使用pip进行第三方库的安装、源码安装和离线whl文件安装。重点讨论了如何通过pip获取whl文件路径并自动化下载,以及使用pipenv创建和管理环境。最后,展示了自动化下载whl文件并使用pip安装的Python脚本示例。
摘要由CSDN通过智能技术生成

1.第三方库安装方式

1.1 pip 安装

以flask为例,使用指令

pip install flask

即可安装
其他选项:

install	安装库
uninstall	卸载库
list	列出已经安装的库
show	列出已安装的库的详细信息
search	通过PyPI搜索库
help	帮助命令

1.2 源码安装

官网获取源文件,进行安装

1.3 pip 离线安装whl

官网获取whl文件进行安装,这样的好处是可以离线安装,但是如果一个一个的获取whl文件,会比较麻烦,而且,不同的包会存在依赖,所以我们最好的方式是通过pip 在线安装,然后获取到whl文件的路径,后按照路径批量下载,后按照顺序进行安装。

2. pipenv环境安装与使用

2.1 pipenv安装和环境创建

pip install pipenv

pipenv环境创建以及使用特定python版本

pipenv  --python=/usr/bin/python3 #指定python版本
pipenv shell # 创建虚拟环境
exit #退出环境
pipenv --rm #删除环境

在这里插入图片描述
在这里插入图片描述

3.flask安装与whl文件路径url导出

3.1 flask 的pip安装

pip list
pip install flask

在这里插入图片描述

3.2 获取whl文件路径并导出url

从中可以看到下载各种库的记录很规整,可以进行使用,要使用的话可以将下载的返回内容导入一个requirement.txt文件内。
指令:pip install flask >> requirement.txt
requirement.txt内容

Looking in indexes: http://pypi.douban.com/simple
Collecting flask
  Downloading http://pypi.doubanio.com/packages/cd/77/59df23681f4fd19b7cbbb5e92484d46ad587554f5d490f33ef907e456132/Flask-2.0.3-py3-none-any.whl (95 kB)
Collecting itsdangerous>=2.0
  Downloading http://pypi.doubanio.com/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
  Downloading http://pypi.doubanio.com/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl (97 kB)
Collecting Werkzeug>=2.0
  Downloading http://pypi.doubanio.com/packages/f4/f3/22afbdb20cc4654b10c98043414a14057cd27fdba9d4ae61cea596000ba2/Werkzeug-2.0.3-py3-none-any.whl (289 kB)
Collecting Jinja2>=3.0
  Downloading http://pypi.doubanio.com/packages/20/9a/e5d9ec41927401e41aea8af6d16e78b5e612bca4699d417f646a9610a076/Jinja2-3.0.3-py3-none-any.whl (133 kB)
Collecting importlib-metadata
  Downloading http://pypi.doubanio.com/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting MarkupSafe>=2.0
  Downloading http://pypi.doubanio.com/packages/e2/a9/eafee9babd4b3aed918d286fbe1c20d1a22d347b30d2bddb3c49919548fa/MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Collecting dataclasses
  Downloading http://pypi.doubanio.com/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl (19 kB)
Collecting zipp>=0.5
  Downloading http://pypi.doubanio.com/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
Collecting typing-extensions>=3.6.4
  Downloading http://pypi.doubanio.com/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Installing collected packages: zipp, typing-extensions, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, Jinja2, itsdangerous, click, flask
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.3 click-8.0.4 dataclasses-0.8 flask-2.0.3 importlib-metadata-4.8.3 itsdangerous-2.0.1 typing-extensions-4.1.1 zipp-3.6.0

4.自动化下载whl与安装

4.1 python环境调用shell指令

参考文章:https://www.cnblogs.com/nwnusun/p/16970717.html

4.2 自动化下载whl程序

代码如下:

import sys
import os
import re
import subprocess

baseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(baseDir)
file_path = 'requirement.txt'
mm = []

def command_func(command):
    #command = command
    #command = 'wget {}'.format(n[2])
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    process.wait()
    # 获取命令的输出和错误信息
    output = process.stdout.read()
    error = process.stderr.read()
    # 将输出和错误信息解码为字符串
    output = output.decode(encoding="utf-8")
    error = error.decode(encoding="utf-8")
    # 返回命令的输出和错误信息
    result = {"output": output, "error": error}
    #print(result)

with open(file_path,mode='rt') as file_object:
    for line in file_object:
        if line.startswith('  Dow'):
        #if 'Downloading' in line:
            #mm.append(line)
            n = re.split(r"\s+",line)
            mm.append(n[2])
            #command = 'wget {}'.format(n[2])
            #command_func(command)

file_pip_path = 'install.txt'
with open(file_pip_path,'wt') as file_object2:
    for url in mm:
        #print(url)
        file = url.split('/')[-1]
        file_object2.write(file+'\n')
        #print(url)
# for url in mm:
#     file = url.split('/')[-1]
#     print(file)

mm = os.listdir()
base_dir = os.path.dirname(os.path.abspath(__file__))
name = 'package'
file_path = os.path.join(base_dir,name)
for file in mm:
    if file.endswith('whl'):
        command = 'mv {} {}'.format(file,file_path)
        command_func(command)

获取到的install.txt文件内容:

Flask-2.0.3-py3-none-any.whl
itsdangerous-2.0.1-py3-none-any.whl
click-8.0.4-py3-none-any.whl
Werkzeug-2.0.3-py3-none-any.whl
Jinja2-3.0.3-py3-none-any.whl
importlib_metadata-4.8.3-py3-none-any.whl
MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
dataclasses-0.8-py3-none-any.whl
zipp-3.6.0-py3-none-any.whl
typing_extensions-4.1.1-py3-none-any.whl

4.3 pip安装whl模块

import os
import subprocess
base_dir = os.path.dirname(os.path.abspath(__file__))
file = 'install.txt'
file_path = os.path.join(base_dir,file)

def command_func(command):
    #command = command
    #command = 'wget {}'.format(n[2])
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    process.wait()
    # 获取命令的输出和错误信息
    output = process.stdout.read()
    error = process.stderr.read()
    # 将输出和错误信息解码为字符串
    output = output.decode(encoding="utf-8")
    error = error.decode(encoding="utf-8")
    # 返回命令的输出和错误信息
    result = {"output": output, "error": error}
    print(result)

with open(file_path,mode='rt') as file_object:
    for file in file_object:
        command = "pip install {}".format(file)
        command_func(command)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值