【python】创建自己的python模块,上传到pypi上

本文详细介绍如何使用Python创建、打包并上传自己的模块至PyPI仓库,包括目录结构搭建、依赖管理、文档编写及版本控制等关键步骤。

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

零、先祭出官网连接

一、创建文件的目录结构

/string2date
  /string2date
    __init__.py
    string2date.py
  setup.py
  LICENSE
  README.md

__init__.py

初始化的数据,之后pip install调包的时候会先加载这里面的数据,所以里面还需要加上

from . import string2date

string2date.py

写的方法

setup.py

from setuptools import setup

setup(name='string2date',
      version='0.0.1',
      description='string to date',
      url='https://github.com/BigDataFounder/string2date',
      author='hurpe',
      author_email='hurpe.ma@gmail.com',
      license='MIT',
      packages=['string2date'])
  • 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下载工具了。

setuptools 和wheel

首先需要保证你有最新版的setuptoolswheel

python -m pip install --user --upgrade setuptools wheel

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

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

python setup.py sdist bdist_wheel

这时会生成两个文件

dist/
  string2date-0.0.1-py2-none-any.whl
  string2date-0.0.1.tar.gz

三、上传档案到Pypi

利用twine将包上传上去

首先先安装twine

python -m pip install --user --upgrade twine

注册一个Pypi的账号

之后会用到帐号密码

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

Tip

  • 不用翻墙
  • 有个邮箱验证,需要登录邮箱验证一下

上传

twine upload dist/*
  • 默认直接上传到了pypi上,如果想指定上传的位置,也可以指定,如下是上传到测试的
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

注意:这地方我遇到了个坑,可能因为我这个电脑python装了好几个版本,还有anaconda,所以我在下载twine的时候不在我配置环境变量的路径里,上传的命令一直不能执行,找了半天又加了一个twine的环境变量才能用

#### 上传成功的命令行

G:\pycode\hurpe\string2date>twine upload dist/string2date-0.0.1*
Enter your username: hurpe
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading string2date-0.0.3-py2-none-any.whl
100%|█████████████████████████████████████| 5.28k/5.28k [00:05<00:00, 1.02kB/s]
Uploading string2date-0.0.3.tar.gz
100%|█████████████████████████████████████| 3.85k/3.85k [00:01<00:00, 2.89kB/s]

四、登录Pypi查看

在这里插入图片描述

五、检验

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

pip install string2date
G:\pycode\hurpe\string2date>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import string2date
>>>

六、更新版本

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

七、更新本地moudle版本

pip install --upgrade string2date

或者是先卸载,再安装

# 卸载string2date
pip uninstall string2date
# 安装string2date
pip install string2date
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云中鲸

谢赏~

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

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

打赏作者

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

抵扣说明:

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

余额充值