使用pypiserver搭建本地源并且远程上传python项目到本地源
文章目录
1 官网地址
https://pypi.org/project/pypiserver/
2 pypiserver安装
2.1 安装pypiserver
1 进入到python虚拟环境
2 安装
# pip install pypiserver
3 新建packages目录
# mkdir -p /Data/python_packages
2.2 添加密码保护
用户:pypi 密码:pypi123456
# pip install passlib
# mkdir -p /etc/pypi
# htpasswd -c /root/.pypipasswd pypi
(如果提示htpasswd命令不存在,使用 yum -y install httpd)
2.3 启动
# nohup pypi-server -p 8585 -P /root/.pypipasswd --fallback-url https://mirrors.aliyun.com/pypi/simple /Data/python_packages > /dev/null &
-p 8585 :使用8585端口
-P /root/.pypipasswd :使用的密码文档
--fallback-url https://mirrors.aliyun.com/pypi/simple:当请求的Python包, 在本地 pypiserver 上没有找到时, 它会将请求转发到外部PyPI源
/Data/python_packages:本地源目录
2.4 访问
http://10.1.212.66:8585/
simple 地址
2.4 更新本地资源库
在pypi-server机器上面
vi /etc/pypi/requirements.txt
amqp==2.5.1
beautifulsoup4==4.8.0
billiard==3.6.1.0
cachetools==3.1.1
celery==4.3.0
celery-once==3.0.1
chardet==3.0.4
下载
pip download -d /Data/python_packages -r /etc/pypi/requirements.txt --index-url https://mirrors.aliyun.com/pypi/simple --extra-index-url https://wheels.galaxyproject.org/simple
重启pypi-server(需要重启,然后本地资源才会被搜索导)
kill pypi-server
nohup pypi-server -p 8585 -P /root/.pypipasswd --fallback-url https://mirrors.aliyun.com/pypi/simple /Data/python_packages > /dev/null &
再次访问http://10.1.212.66:8585/simple/ 本地库中已经有了
3 客户端使用
3.1 下载本地源中没有的包
1 搜索flask发现没有
(python3) 饪? ~ pip search -i http://10.1.22.66:8585 flask
2 下载
vi requirements.txt
flask==1.0.4
pip install -i http://10.1.212.66:8585 -r requirements.txt --trusted-host 10.1.212.66 。–trusted-host 10.1.212.66 添加信任主机,如果不是https访问需要添加这个一项。可以看到连接到了http://10.1.212.66:8585。但是下载还是到了/mirrors.aliyun.com。因为本地没有。【Downloading https://mirrors.aliyun.com】
(python3) 饪? ~ pip install -i http://10.1.212.66:8585 -r requirements.txt --trusted-host 10.1.212.66
Looking in indexes: http://10.1.212.66:8585
Collecting flask==1.0.4
Downloading https://mirrors.aliyun.com/pypi/packages/d8/94/7350820ae209ccdba073f83220cea1c376f2621254d1e0e82609c9a65e58/Flask-1.0.4-py2.py3-none-any.whl (92 kB)
|################################| 92 kB 900 kB/s
Requirement already satisfied: itsdangerous>=0.24 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (1.0.0)
Requirement already satisfied: Jinja2>=2.10 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (2.10.3)
Requirement already satisfied: click>=5.1 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from Jinja2>=2.10->flask==1.0.4->-r requirements.txt (line 1)) (1.1.1)
Installing collected packages: flask
Attempting uninstall: flask
Found existing installation: Flask 1.1.1
Uninstalling Flask-1.1.1:
Successfully uninstalled Flask-1.1.1
Successfully installed flask-1.0.4
3.2 下载本地源中有的包
先通过2.4步骤先下载包到本地,这里下载 elasticsearch==7.5.1
1 查询发现有7.5.1版本
(python3) 饪? ~ pip search -i http://10.1.212.66:8585 elasticsearch
elasticsearch (7.5.1) - 7.5.1
(python3) 饪? ~
2 安装。发现下载【Downloading http://10.1.212.66:8585/packages/】
pip install -i http://10.1.212.66:8585 -r requirements.txt --trusted-host 10.1.212.66
Looking in indexes: http://10.1.212.66:8585
Requirement already satisfied: flask==1.0.4 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (1.0.4)
Collecting elasticsearch==7.5.1
Downloading http://10.1.212.66:8585/packages/elasticsearch-7.5.1-py2.py3-none-any.whl (86 kB)
|################################| 86 kB 3.8 MB/s
Requirement already satisfied: Jinja2>=2.10 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (2.10.3)
Requirement already satisfied: click>=5.1 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (7.0)
Requirement already satisfied: itsdangerous>=0.24 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from flask==1.0.4->-r requirements.txt (line 1)) (1.0.0)
Requirement already satisfied: urllib3>=1.21.1 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from elasticsearch==7.5.1->-r requirements.txt (line 2)) (1.25.3)
Requirement already satisfied: MarkupSafe>=0.23 in ./.pyenv/versions/3.7.4/envs/python3/lib/python3.7/site-packages (from Jinja2>=2.10->flask==1.0.4->-r requirements.txt (line 1)) (1.1.1)
Installing collected packages: elasticsearch
Successfully installed elasticsearch-7.5.1
(python3) 饪? ~
4 上传python项目到pypi
4.1 目录结构
参考文档《打包Python项目》:https://packaging.python.org/tutorials/packaging-projects/
4.2 安装打包工具
pip install setuptools wheel
4.3 创建README.md
echo "login" >> README.md
4.4 创建LICENSE(版权协议)
echo "LICENSE" >> LICENSE
4.5 创建setup.py
vi setup.py
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example-pkg", # Replace with your own username
version="0.0.1",
author="Example Author",
author_email="aaa@qq.com",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
4.6 创建本地登录用户
vi ~/.pypirc
[distutils]
index-servers = pypihost
[pypihost]
repository: http://10.1.22.66:8585
username: pypi
password: pypi123456
4.6 打包并且上传
python setup.py bdist_wheel upload -r pypihost (只会有.whl文件)
(python setup.py sdist bdist_wheel upload -r pypihost 会有.tar.gz和.whl两种文件)
4.6 其它客户端安装上传的包
1 查看
(python3) 饪? pypi_test pip search -i http://10.1.212.66:8585 example_pkg
example_pkg (0.0.1) - 0.0.1
(python3) 饪? pypi_test
2 安装
vi requirements.txt
(python3) 饪? ~ cat requirements.txt
example_pkg==0.0.1
(python3) 饪? ~
3 下载
(python3) 饪? ~ pip install -i http://10.1.212.66:8585 -r requirements.txt --trusted-host 10.1.212.66
Looking in indexes: http://10.1.212.66:8585
Collecting example_pkg==0.0.1
Downloading http://10.1.212.66:8585/packages/example_pkg-0.0.1-py3-none-any.whl (2.4 kB)
Installing collected packages: example-pkg
Successfully installed example-pkg-0.0.1
(python3) 饪? ~ cat requirements.txt
example_pkg==0.0.1
(python3) 饪? ~
4 查看代码
参考文档
《使用pypi-server搭建简单的PyPI源》:https://www.bbsmax.com/A/QW5YMgg35m/
《基于 pypiserver 的 PyPI 私有仓库搭建实践》:https://www.jianshu.com/p/c260b59cd3d0
《打包Python项目》:https://packaging.python.org/tutorials/packaging-projects/
技术交流
CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!