Python 打包PyPI 上传

一、 文件目录结构示例:

full-month/
├── LICENSE
├── README.md
├── full_month
│   └── __init__.py
└── setup.py

full_month文件夹中的 __init__.py 是必需的。且在该示例中,我的代码是写在了这个 __init__.py 文件中。

__init__.py is required to import the directory as a package, and can simply be an empty file. 

setup.py 

from setuptools import setup, find_packages  # type: ignore

setup(name='full-month',    # pip install 时用的就是这个名字
      version='0.0.1',
      description='show the full display of one month of 5 * 7 format',
      author='neomaple',
      author_email='xxx@163.com',
      license='None',
      packages=find_packages())
  • name
    • 就是包名
  • version
    • 包的版本号
  • packages
    • 可引入的包的名字,可以使用setuptools.find_packages()去自动导入

README.md
自定义的一项,可以简单描述一下包是做什么的

LICENSE
许可证,详细信息可以去官网查看,如果用的是MIT,可以直接复制下面的。
Copyright (c) 2018 The Python Packaging Authority

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.

二、 生成档案

如果上述工作都做完了,那么就可以开始准备上传包了,之后就可以用pip install下载工具了。

1. setuptools 和wheel

首先需要保证你有最新版的setuptools 和wheel
python -m pip install --user --upgrade setuptools wheel

Tip:如果出现问题了可以查看官网的解决方案:https://packaging.python.org/tutorials/installing-packages/

2. 进入到和setup.py同级目录下

python setup.py sdist bdist_wheel

这时会生成三个文件夹:

├── build
│   ├── bdist.macosx-10.6-intel
│   └── lib
│       └── full_month
│           └── __init__.py
├── dist
│   ├── full-month-0.0.1.tar.gz
│   └── full_month-0.0.1-py3-none-any.whl
├── full_month.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt

三、上传档案到Pypi

1. 利用twine将包上传上去
首先先安装twine
python -m pip install --user --upgrade twine

2. 注册一个Pypi的账号

之后会用到帐号密码

网站传送门:https://pypi.org/

3. 上传

twine upload dist/*    # 默认是上传到了 pypi

四、登录Pypi查看

五、检验

这时候就可以,下载包,然后运行里面方法了

pip install full-month

导入:

In [1]: import full_month                                                                                                                                     

In [2]:                                                                                                                                                       

In [2]: full_month.monthcalendar(2021, 1)                                                                                                                     
Out[2]: 
[[28, 29, 30, 31, 1, 2, 3],
 [4, 5, 6, 7, 8, 9, 10],
 [11, 12, 13, 14, 15, 16, 17],
 [18, 19, 20, 21, 22, 23, 24],
 [25, 26, 27, 28, 29, 30, 31]]

六、更新版本

  • 更新版本也很简单,只需要修改setup.py下的version
  • 然后重新生成档案,上传
python setup.py sdist bdist_wheel
twine upload dist/string2date-0.0.2*

七、更新本地module版本

pip install --upgrade full-month

或者是先卸载,再安装

# 卸载full-month
pip uninstall full-month
# 安装full-month
pip install full-month

参考链接:

https://blog.csdn.net/qq_38486203/article/details/83659287

https://segmentfault.com/a/1190000008663126

官方文档:

https://packaging.python.org/tutorials/packaging-projects/

原文地址:https://www.cnblogs.com/neozheng/p/14238777.html