搭建本地的pip镜像源

1. 创建文件夹:./pypi_mirror_test

2. 创建并进入conda虚拟环境,安装pip2pi包

pip install pip2pi

3. 下载pypi的packages

可以参考其他博客,或者我之前的博客.

偷懒,仅仅测试用的话:

1)在文件夹下创建requirements.txt文本文件,内容如下

requirements.txt

beautifulsoup4==4.11.1
bs4==0.0.1
certifi 
charset-normalizer==2.1.1
idna==3.4
lxml==4.9.1
numpy==1.21.6
pandas==1.3.5
pip2pi==0.8.2
python-dateutil==2.8.2
pytz==2022.6
requests==2.28.1
six==1.16.0
soupsieve==2.3.2.post1
urllib3==1.26.13
wincertstore==0.2

2)在命令行中,进入pypi_mirror_test目录所在文件夹,执行以下命令, 安装几个pypi数据包

pip install -r ./pypi_mirror_test/requirements.txt

4. 执行dir2pi命令,生成pypi包的html索引

说明:

  • 1)dir2pi函数是pip2pi包中自带的,不需要额外安装。
  • 2)在ubuntu下运行需要加 sudo 命令。
  • 3)在windows下运行,需要“以管理员身份”命令行,否则会报错。

在linux中:

sudo dir2pi ./pypi_mirror_test

在windows中:以管理员身份打开cmd命令行终端,执行

dir2pi ./pypi_mirror_test

执行完毕后,会在./pypi_mirror_test文件夹下生成一个simple文件夹,存储着python包的html索引。

5. 发布镜像源(暴露资源至网络),使得互联网或局域网可以访问下载package

方式1:用python创建server服务

simple或者在site-packages 里创建server服务,8080为端口号,可以随意设置:

python -m http.server 8080

 此时可以在浏览器中访问并下载自己建立的pypi镜像库

路径1:127.0.0.1:8080

路径2:your_compuer_ip:8080. 其中,your_compuer_ip是你电脑的ipv4的地址,可以通过命令行终端自行查看。

方式2: 用nginx发布

下载安装nginx,修改nginx.conf:nginx下载地址

server{
    listen		8080;  # 1.设置端口
    server_name 	127.0.0.1;  # 2.设置网址名
    access_log 	logs/pip.log;
    location / {
            root D:/program/pypi_mirror_test/simple; # 3.设置html文件的搜索路径
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
    }
}

双击执行nginx.exe启动nginx,此时可以用浏览器打开127.0.0.1:8080访问simple文件夹,

但是我这里总是下载文件失败,还没找到原因。

【错误原因】可能是没有配置好nginx对“.symlink”文件的权限。通过修改simple文件夹下的index.html文件可以绕过.symlink文件,直接下载源文件。具体修改方法是:

需要去修改simple目录下每个软件包目录下的index.html文件(此部分修改可自行写个python脚本批处理下):
手动操作示范:

在这里插入图片描述

修改前:
在这里插入图片描述

修改后:
在这里插入图片描述

用python脚本批量修改:

import os 
import glob
 
 
class Class_Util_Text():
    def __init__(self):
        pass
 
    def read_text(self,text_path):
        """
        读取本地text文件到列表,并返回该列表
        """
        assert os.path.exists(text_path)
        with open(text_path,"r") as f:
            lines=f.readlines() #读行
        return lines
 
    def write_text(self,text_path,text_content):
        """
        把文字写入文本文件中,会清空文本文件的原有内容
        """
        with open(text_path,"w") as f:
            f.writelines(text_content)
            f.writelines("\n")
 
    def append_text(self,text_path,text_content):
        """
        把文字添加到文本文件的末尾,保留原文本文件内容
        """
        with open(text_path,"a") as f:
            f.writelines(text_content)
            f.writelines("\n")
 
    def replace_text(self,text_path,replace_old,replace_new):
        """
        替换文本文件中的部分文字,先读文件,替换文字后,再重新写入文件
        """
        #read
        with open(text_path,"r") as f:
            lines=f.readlines() 
            data=[]  
            for i in lines:
                #根据条件修改
                if(replace_old in i):
                    i=i.replace(replace_old,replace_new)   #修改 replace_old 为 replace_new
                data.append(i)     #记录每一行      
        #write
        with open(text_path,"w") as f:
            for i in data:
                f.writelines(i)
 
 
 
if __name__ =="__main__":
    txt_obj = Class_Util_Text()
    dir_path = "D:\pypi-packages\simple"
    html_files = glob.glob(dir_path+"/*/index.html")
 
    for html_path in html_files:
        txt_obj.replace_text(html_path,replace_old="<a href='",replace_new="<a href='../../" )
        # txt_obj.replace_text(html_path,replace_old="<a href='../../",replace_new="<a href='" )
        lines = txt_obj.read_text(html_path)
        print(html_path)
        print(lines)

方式3:pypi-server命令

先安装pypiserver

pip install pypiserver

然后执行下面命令: 

pypi-server -p 9090 -P your_path_to_pypi_dir

命令解析:

        -p:指定端口。不指定的话,pypi-server的默认端口是8080.

        -P:指定package路径。不指定的话,pypi-server的默认路径是“~/packages”

  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值