当前位置:   article > 正文

搭建企业内网pypi镜像库,让python在内网也能像互联网一样安装pip库_搭建内网pypi源

搭建内网pypi源

知识点

PyPI和pip是两个不同的概念,但它们是密切相关的。

PyPI(Python Package Index)是一个Python软件包仓库,Python开发人员可以在其中发布、分享和下载Python软件包。开发人员可以使用Web界面或命令行工具上传软件包到PyPI,然后其他开发人员就可以使用pip(Python Package Installer)这个包管理工具从PyPI上下载和安装软件包。

官方:https://pypi.org/simple
清华:https://pypi.tuna.tsinghua.edu.cn/simple
百度:https://mirror.baidu.com/pypi/simple/
阿里:https://mirrors.aliyun.com/pypi/simple/
豆瓣:https://pypi.douban.com/simple/
中科大:https://pypi.mirrors.ustc.edu.cn/simple/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例

pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple

大体分为以下步骤:

  1. 服务器安装python
  2. 新建一个目录/mirror/pip,用于存储pypi文件,作为仓库目录
  3. 下载python中的所需包放至仓库文件夹/mirror/pip
    1. 新建requirement.py脚本(将清华pypi镜像库文件列表粘贴到requirement.txt文件中)
    2. 新建download.py脚本(依据requirement.txt下载pypi镜像库)

实验

1.服务器安装python及pip2pi

dnf install -y python
pip3 install pip2pi
  • 1
  • 2

2.新建一个目录/mirror/pip,用于存储pypi文件,作为仓库目录

mkdir /mirror
mkdir /mirror/pip
  • 1
  • 2

3.下载python中的所需包放至仓库文件夹/mirror/pip

3.1. 新建requirement.py脚本(将清华pypi镜像库文件列表粘贴到requirement.txt文件中)
touch requirement.py
  • 1
import requests
import re
report = requests.request('get','https://mirrors.bfsu.edu.cn/pypi/web/simple')
# print(report.text)
text_str = str(report.text).split('\n')
with open('requirement.txt','w+') as f:
    for i in text_str:
        temp = re.findall('<a href="(.*?)/">',i)
        # print(i,temp)
        if temp != []:
            f.write(str(temp[0])+'\n') 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

3.2. 新建download.py脚本(依据requirement.txt下载pypi镜像库)
touch download.py
  • 1
import os
file="./requirement.txt"

#使用 with 语句可以避免忘记关闭文件而导致的资源泄露问题。 
#open(file, 'r+'):这是调用 open() 函数来打开文件。file 是文件路径和名称的字符串,'r+'模式表示文件以读写模式打开
#as f:这表示将打开的文件对象赋值给变量 f,你可以在随后的代码块中使用这个变量来操作文件。

with open(file,'r+') as f:
    text = f.readlines()
    for i in text:
        if not os.path.exists(i[:-1]):
#i[:-1] 是一个Python的切片操作,它返回字符串 i 的所有字符,除了最后一个。(在Windows上是\,在大多数其他系统上是/)
            os.mkdir(i[:-1])
            os.system('pip download '+i[:-1]+' -i https://mirrors.bfsu.edu.cn/pypi/web/simple -d '+i[:-1])
        else:
            print(f"{i[:-1]} 文件夹已存在,本次跳过。")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

4.执行requirement.py

cd /mirror/pip
python requirement.py
  • 1
  • 2

5.执行download.py

cd /mirror/pip
python download.py
  • 1
  • 2

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

6.安装并配置nginx

详见:https://blog.csdn.net/xzzteach/article/details/137182578
第四点
在这里插入图片描述

7.使用配置

临时配置:

pip install django -i http://192.168.200.8/
  • 1

永久配置:

pip config set global.index-url http://192.168.200.8/
pip install django
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/973131
推荐阅读
相关标签
  

闽ICP备14008679号