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

知识点

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/

示例

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

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

mkdir /mirror
mkdir /mirror/pip

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

3.1. 新建requirement.py脚本(将清华pypi镜像库文件列表粘贴到requirement.txt文件中)
touch requirement.py
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') 

在这里插入图片描述

3.2. 新建download.py脚本(依据requirement.txt下载pypi镜像库)
touch download.py
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]} 文件夹已存在,本次跳过。")

在这里插入图片描述

4.执行requirement.py

cd /mirror/pip
python requirement.py

5.执行download.py

cd /mirror/pip
python download.py

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

6.安装并配置nginx

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

7.使用配置

临时配置:

pip install django -i http://192.168.200.8/

永久配置:

pip config set global.index-url http://192.168.200.8/
pip install django
  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 CentOS 上搭建 PyPI 镜像,您可以按照以下步骤进行操作: 1. 安装并配置 Nginx 服务器 在 CentOS 上安装 Nginx: ``` sudo yum install nginx ``` 配置 Nginx 服务器,将其作为 PyPI 镜像的代理服务器。在 Nginx 的配置文件 `/etc/nginx/nginx.conf` 中添加以下内容: ``` server { listen 80; server_name pypi.example.com; access_log /var/log/nginx/pypi.access.log; error_log /var/log/nginx/pypi.error.log; location / { proxy_pass http://pypi.python.org; proxy_set_header Host pypi.python.org; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` 注意将 `pypi.example.com` 替换为您的 PyPI 镜像域名。 2. 安装并配置 devpi-server devpi-server 是一个 PyPI 服务器,您可以使用它来创建本地 PyPI 镜像。在 CentOS 上安装 devpi-server: ``` sudo yum install python3-pip sudo pip3 install -U devpi-server ``` 创建一个 devpi-server 实例: ``` devpi-server --start --host=127.0.0.1 --port=3141 ``` 您可以使用 `--port` 参数指定 devpi-server 实例的端口号。 3. 配置 pip 在您的 CentOS 机器上,打开 `~/.pip/pip.conf` 文件,如果没有该文件则创建它,添加以下内容: ``` [global] index-url = http://pypi.example.com/simple/ trusted-host = pypi.example.com ``` 注意将 `pypi.example.com` 替换为您的 PyPI 镜像域名。现在,当您使用 pip 安装 Python 包时,它将从您的本地 PyPI 镜像获取软件包。 希望这些步骤可以帮助您在 CentOS 上搭建 PyPI 镜像
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值