想要把自己开发的库分享给别人使用, 使用 pip install
命令来安装 , 需要学习如何制作一个python 安装包
一、注册pypi账号
https://pypi.org/account/register/
二、创建setup.py和pypirc文件
setup.py模板(该文件放在项目根目录下)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
from os.path import abspath, dirname, join from setuptools import setup, find\_packages \ # 获取requirements.txt里的依赖信息 install\_reqs = \[req.strip() for req in open (abspath(join(dirname(\_\_file\_\_), 'requirements.txt' )))\] with open ( "README.md" , 'r' , encoding = "utf-8" ) as f: long \_description = f.read() setup( version = '0.0.1' , packages = find\_packages(), url = '网址' , license = '协议' , author = '作者姓名' , author\_email = '作者邮箱' , description = '描述信息' , long \_description = long \_description, long \_description\_content\_type = "text/markdown" , install\_requires = install\_reqs, ) |
pypirc模板 (该文件放在家目录内)
这个文件用来存储刚才注册pypi账号信息
1
2
3
4
5
6
|
[distutils] index - servers = pypi [pypi] repository = https: / / upload.pypi.org / legacy / username = 刚才注册的用户名 password = 刚才注册的密码 |
三、安装依赖
1
|
pip install --upgrade pip twine wheel setuptools |
四、打包
1
|
python setup.py sdist bdist_wheel |
打包之后 会在项目的dist目录内生成whl文件
五、将whl文件上传到pypi服务器
1
|
twine upload dist / * |
以上就是Python如何将模块打包并发布的详细内容,更多关于python 模块打包发布的资料请关注服务器之家其它相关文章!
原文链接:https://segmentfault.com/a/1190000023821700?utm_source=tuicool&utm_medium=referral