Python搭建私有pypi源发包——pypi-server

本文详细介绍了如何在Windows环境下搭建私有PyPI源,包括安装所需软件、创建密码文件、启动服务器。接着,展示了如何开发Python包,编写setup.py文件,生成dist目录并上传到私有源。最后,演示了如何在本地安装和搜索自定义发布的Python包。整个过程涵盖了Python包的完整生命周期,从创建到发布的各个环节。

简介

本地搭建 pypi 源,开发包,并在私有源上发包

以 Windows 为例,以管理员身份运行 PowerShell




1. 搭建私有 pypi 源

安装

pip install pypiserver
mkdir ~/packages
pip install passlib
pip install setuptools
pip install wheel
pip install twine

htpasswd 在线生成器

创建密码文件

New-Item ~/.pypipasswd
Set-Content ~/.pypipasswd '以上生成结果'

启动

pypi-server -P C:\Users\Administrator\.pypipasswd

访问 http://localhost:8080/

可用 Supervisor 启动守护进程,此处略




2. 开发要上传的包

新建项目 mytool

mytool.py

def show():
    print('Hello World!')

__init__.py

from mytool import *

LICENSE,可参考 Choose an open source license

The MIT License (MIT)
Copyright (c) 2013 Steve Canny, https://github.com/scanny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

README.md

# 简介

我的工具

setup.py,可参考 python-docx/setup.py

from setuptools import find_packages, setup

setup(
    name='mytool',
    version='0.0.1',
    description='我的工具',
    author='XerCis',
    author_email='',
    url='项目地址',
    license='MIT License',
    packages=find_packages(exclude=['build', 'dist', 'tests', 'tests.*']),
    include_package_data=True,
    install_requires=[],
    zip_safe=False,
)




3. 生成 dist 目录用于上传

python setup.py sdist bdist_wheel




4. 上传包到私有 pypi 源

修改 pypirc 设置

cat ~/.pypirc

如下

[distutils]
index-servers =
  pypi
  local

[pypi]
username: 在pypi上的用户名
password: 在pypi上的密码

[local]
repository: http://localhost:8080
username: 用户名
password: 密码

上传到 local 仓库

twine upload dist/* -r local

访问 http://localhost:8080/simple/




5. 安装

查找包

pip search -i http://localhost:8080 mytool

安装包

pip install -i http://localhost:8080/simple mytool
pip install -U -i http://localhost:8080/simple mytool

安装最新包

pip install -U -i http://localhost:8080/simple mytool

测试

from mytool import mytool

mytool.show()




Linux 安装

创建密码那步可以这样:

安装工具包

sudo apt-get install -y apache2-utils

创建用户名和密码

htpasswd -c ~/.pypipasswd XerCis

查看

cat ~/.pypipasswd




参考文献

  1. Python 软件包打包指南
  2. pypi-server GitHub
  3. setuptools Documentation
  4. twine Documentation
  5. 基于 pypiserver 的 PyPI 私有仓库搭建实践
  6. 如何搭建自己的pypi私有源服务器
  7. pypi发包教程
  8. Python打包工具setuptools的使用
  9. python分发包管理
  10. Python编程:twine模块打包python项目上传pypi
  11. Python编程:为世界贡献你的轮子-pipy打包
  12. 使用pypi-server搭建简单的PyPI源
  13. htpasswd - 管理用于basic认证的用户文件
  14. Powershell - 创建文本文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值