自定义package上传到 pypi

本文详细介绍如何使用Twine将Python包上传到PyPI。包括安装Twine、设置项目结构、创建必要的配置文件如README.rst、MANIFEST.in、setup.py等,并讲解如何构建源码分发和轮子文件,最后完成上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to upload package to pypi

Install twine

pip install twine

Init file

First of all, make sure that the structure of project is correct. Says, your project code in a folder call ‘foo’, then you have README and some other files in the same level.

For example,

.
├── foo
│ └── a.py
└── readme.md
Secondly, add specify file to the root folder.

  • README.rst
  • MANIFEST.in
  • setup.py
  • setup.cfg(option)
    pypi do not know markdown format readme, there is only rst format available!!!
    由于pypi不能使用markdown格式的readme,因此需要用rst格式,rst语法有共通之处,详见这篇博文

setup.py

look at
https://github.com/pypa/sampleproject/blob/master/setup.py

after finish setup.py
you can use

pip install -e .
install package locally on editable mode.
It will also install dependency.

Make dist

1 Build source distribution

run:
python setup.py sdist

2 Build wheel.

If your code compat py2 and py3:
python setup.py bdist_wheel --universal

else:
python setup.py bdist_wheel

if your code is specify for some python version, add python tag in setup.cfg

For example, if your code is only available in py3.5 and 3.4

[bdist_wheel]
python-tag = py35, py34

Upload

look at http://python-packaging-user-guide.readthedocs.io/en/latest/distributing/#uploading-your-project-to-pypi

Create a account on pypi, register your package, write a ~/.pypirc
[distutils]
index-servers=pypi

[pypi]
repository = https://pypi.python.org/pypi
username = <username>
password = <password>

left the password empty, you can use twine with -p PASSWORD

upload your dist folder

twine upload dist/*

Reference

http://python-packaging-user-guide.readthedocs.io/en/latest/distributing/
http://peterdowns.com/posts/first-time-with-pypi.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值