暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

搭建私有PYPI源

生有可恋 2022-07-29
1559

国内已经有很多Python包的镜像源,总的PYPI镜像大小约为11.8T。如果需要经常下载Python包还是有必要在企业内部搭一个私有的镜像源。


我们选择从官网同步镜像,使用的工具是 bandersnatch,其中 bandersnatch 对 Python 的版本依赖为 >= Python 3.8.0


使用方法:

    $ python --version
    Python 3.8.0
    $ pip3 install --upgrade pip
    $ pip3 --version
    pip 22.2.1 from home/hyang0/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pip (python 3.8)
    $ pip list
    Package Version
    ---------- -------
    pip 22.2.1
    setuptools 41.2.0
    $ pip3 install bandersnatch
    $ bandersnatch -c bandersnatch.cfg mirror
    2022-07-28 14:16:45,121 WARNING: Config file 'bandersnatch.cfg' missing, creating default config. (main.py:200)
    2022-07-28 14:16:45,121 WARNING: Please review the config file, then run 'bandersnatch' again. (main.py:201)

    安装完 bandersnatch 后就可以同步了,第一次同步需要创建配置文件,-c 参数即是指定配置文件位置。当配置文件不存在时,同步程序会自动创建默认配置文件。我们需要对默认配置文件进行修改:

      [mirror]
      directory = srv/pypi
      json = false
      release-files = true
      cleanup = false
      master = https://pypi.org
      timeout = 10
      global-timeout = 1800
      workers = 3
      hash-index = false
      stop-on-error = false
      storage-backend = filesystem
      verifiers = 3
      compare-method = hash


      其中默认同步路径的参数为 directory, 这个参数可以设置为我们自己的目录。设置完参数,我们再次运行上述命令即可开始同步:

        $ bandersnatch -c bandersnatch.cfg mirror


        同步后的目录结构为:

          .
          ├── generation
          ├── todo
          └── web
          ├── local-stats
          ├── packages
          └── simple


          我们本地如何测试搭建的镜像同步站点是可用的?可以使用 pip 配置同步路径进行测试:

            $ pip config debug
            env_var:
            env:
            global:
            etc/xdg/pip/pip.conf, exists: False
            etc/pip.conf, exists: False
            site:
            home/hyang0/.pyenv/versions/3.8.0/pip.conf, exists: False
            user:
            home/hyang0/.pip/pip.conf, exists: False
            home/hyang0/.config/pip/pip.conf, exists: False
            $ pip config --site set global.index-url g/rsync/pypi/web/simple
            Writing to /home/hyang0/.pyenv/versions/3.8.0/pip.conf
            $ pip config list
            global.index-url='/g/rsync/pypi/web/simple'

            配置好 pip 之后就可以尝试安装一个包,看能否从本地源下载文件:

              $ pip install 0
              WARNING: The index url "/g/rsync/pypi/web/simple" seems invalid, please provide a scheme.
              Looking in indexes: /g/rsync/pypi/web/simple
              Processing /g/rsync/pypi/web/packages/8c/e6/83748ba1e232167de61f2bf31ec53f4b7acdd1ced52bdf3ea3366ea48132/0-0.0.0-py2.py3-none-any.whl
              Installing collected packages: 0
              Successfully installed 0-0.0.0


              这里通过文件系统的方式指定 pip 源的 URL,后期可以通过Web形式将目录发布出去,这样就形成了一个内部镜像站点。


              如果需要将 pip 源换成正常的,使用如下命令:

                $ pip config --site set global.index-url https://mirrors.aliyun.com/pypi/simple/
                Writing to /home/hyang0/.pyenv/versions/3.8.0/pip.conf
                $ pip config --site set install.trusted-host mirrors.aliyun.com
                Writing to /home/hyang0/.pyenv/versions/3.8.0/pip.conf
                $ pip config list
                global.index-url='https://mirrors.aliyun.com/pypi/simple/'
                install.trusted-host='mirrors.aliyun.com'
                $ pip uninstall 0
                Found existing installation: 0 0.0.0
                Uninstalling 0-0.0.0:
                Would remove:
                /home/hyang0/.pyenv/versions/3.8.0/lib/python3.8/site-packages/0-0.0.0.dist-info/*
                /home/hyang0/.pyenv/versions/3.8.0/lib/python3.8/site-packages/0.py
                Proceed (Y/n)? y
                Successfully uninstalled 0-0.0.0


                最后再说说 bandersnatch 的其它子命令,它有四个子命令,分别是:

                • mirror  镜像同步,中断后可重复执行

                • verify   验证元数据完整性

                • sync     同步单个包

                • delete  删元数据


                全文完。


                如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。


                文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                评论