pip离线装包

  • 前言
    python开发中对一些无网的环境,需要安装python包怎么办?有三种方式
  • 第一种: 手动安装
    • pypi官网(国外网站一般不通)
    • 或者国内镜像源
      (1)阿里云 http://mirrors.aliyun.com/pypi/simple/
      (2)豆瓣http://pypi.douban.com/simple/
      (3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
      (4)中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
      (5)华中科技大学http://pypi.hustunique.com/
      
    比如要下载requests包直接访问http://mirrors.aliyun.com/pypi/simple/requests,可以看到如下画面
    在这里插入图片描述
    此时可以选择版本下载,然后解压安装,这一步不赘述
    缺点就是无法自动安装依赖,需要手动安装每一个依赖,这个第二种方式可以自动收集依赖。

  • 第二种: pip download+install
    本质同第一步,只是可以采用批量脚本来收集库和依赖
    下面给出一个可用脚本, 改巴改巴就能用, 比如在开发环境download到需要的包和依赖,到生产环境直接install
    """
    自动根据脚本配置安装 python包
    python第三方包如何安装?
    开发端包收集
    pip download -i "https://mirrors.aliyun.com/pypi/simple/" -d ./ lxml  - 下载 `lxml` 包
    pip download -i "https://mirrors.aliyun.com/pypi/simple/" -d ./ -r requirement.txt - 下载 requirement.txt
    
    生产端
    pip install --no-index --ignore-installed --find-links=./ lxml
    pip install --no-index --ignore-installed --find-links=./ -r requirements.txt
    """
    import os
    import subprocess
    import traceback
    
    
    class Pip:
        def __init__(self, path):
            self.soft_source = "https://mirrors.aliyun.com/pypi/simple/"
            self.package_pre = f"{path}/models"
    
        def _venv(self, model: str):
            base_path = os.path.join(self.package_pre, model)
            path = os.path.join(os.path.join(base_path, "my_venv"))
            if not os.path.exists(path):
                os.makedirs(path)
                with open("__init__.py", "w"):
                    pass
            else:
                if not os.path.exists(os.path.join(path, "__init__.py")):
                    with open("__init__.py", "w"):
                        pass
    
        def download(self, package: str, model: str, version=None):
            base_path = os.path.join(self.package_pre, model)
            path = os.path.join(os.path.join(base_path, "package", package))
            if not os.path.exists(path):
                os.makedirs(path)
            if not package.endswith("txt"):
                command = f'pip3 download -i "{self.soft_source}" -d {path} {package} --timeout=100'
                if version:
                    command += f"=={version}"
    
            else:
                command = f'pip3 download -i "{self.soft_source}" -d {path} -r {os.path.join(base_path, package)}'
            print(f"download:: command={command}")
            res = subprocess.run(command,
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                 encoding="utf-8", timeout=100, shell=True)
            print(res.returncode, res.stdout, res.stderr)
            return res
    
        def install(self, package: str, model: str, version=None):
            base_path = os.path.join(self.package_pre, model)
            path = os.path.join(base_path, "package", package)
            if not os.path.exists(path):
                raise FileNotFoundError(path)
            if not package.endswith("txt"):
                command = f'pip3 install --no-index --ignore-installed --find-links={path} {package}' + "=={version}" if version else ""
            else:
                command = f'pip3 install --no-index --ignore-installed --find-links={path} -r {os.path.join(base_path, package)}'
            print(command)
            res = subprocess.run(command,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 encoding="utf-8",
                                 timeout=100,
                                 shell=True)
            if res.returncode == 0:
                print(f"code={res.returncode}, stdout={res.stdout}, stderr={res.stderr}")
                return self.check(package)
            else:
                print(f"code={res.returncode}, stdout={res.stdout}, stderr={res.stderr}")
                return 0
    
        @staticmethod
        def check(package):
            res = subprocess.run(f"pip3 show {package}",
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                 encoding="utf-8", timeout=10, shell=True)
            print(res.returncode)
            if res.returncode == 0:
                print("res.stdout: {}".format(res.stdout.split('\n')))
                return 1
            else:
                return 0
    
        def upgrade_pip(self):
            res = subprocess.run(f"pip3 install --upgrade pip", stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE, encoding="utf-8", timeout=10, shell=True)
    
        @staticmethod
        def update(stdout: str):
            if stdout.find("pip install --upgrade pip") != -1:
                res = subprocess.run(f"python3 -m pip install --upgrade pip",
                                     stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                     encoding="utf-8", timeout=10, shell=True)
                if res.returncode == 0:
                    return 1
    
        @staticmethod
        def access():  # 检测pip和python是否存在
            try:
                res = subprocess.run(f"pip3 -V", stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE, encoding="utf-8", timeout=10, shell=True)
    
                subprocess.run(f"pip3 list >> hahahahahha.txt", stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE, encoding="utf-8", timeout=10, shell=True)
            except FileNotFoundError as err:
                print(traceback.format_exc())
                pass
            else:
    
                if res.returncode == 0:
                    if res.stdout.find("python 3") != -1:
                        try:
                            res = subprocess.run(f"python3 -V",
                                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                 encoding="utf-8", timeout=10, shell=True)
                        except FileNotFoundError as err:
                            pass
                        else:
                            if res.returncode == 0:
                                if res.stdout.find('Python 3') != -1:
                                    return 1
    
        def __call__(self, package: str, model: str, version=None, cmd="download"):
            # 检查 pip和 python环境
            if self.access():
                if cmd == "download":
    
                    return self.download(package=package, model=model, version=version)
                else:
    
                    return self.install(package=package, model=model, version=version)
            else:
                print(f"python3 or pip3 环境异常")
    
    
    if __name__ == '__main__':
        pipe = Pip("../")
        # pipe(model="demo", package="requests")
    
        pipe.download()
    
    

  • 第三种: 配置代理

    • 1.命令行指定
      pip install xx --proxy <proxy>
      
    • 2.全局配置
      ~/.pip/pip.conf
      [global]
      timeout = 3000
      index-url = https://mirrors.aliyun.com/pypi/simple
      proxy = xx
      [install]
      trusted-host=mirrors.aliyun.com
      
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值