简介
本地搭建 pypi 源,开发包,并在私有源上发包
以 Windows 为例,以管理员身份运行 PowerShell
1. 搭建私有 pypi 源
安装
pip install pypiserver
mkdir ~/packages
pip install passlib
pip install setuptools
pip install wheel
pip install twine
创建密码文件
New-Item ~/.pypipasswd
Set-Content ~/.pypipasswd '以上生成结果'
启动
pypi-server -P C:\Users\Administrator\.pypipasswd

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

2484

被折叠的 条评论
为什么被折叠?



