翼度科技»论坛 编程开发 python 查看内容

【python教程】打包和发布自己的项目,让别人去pip

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
@
目录

1.环境搭建

1.1 换源


  • 在pip安装时使用-i参数,可以指定源。以下有许多种国内源可以选择
  1. https://pypi.tuna.tsinghua.edu.cn/simple
  2. http://mirrors.aliyun.com/pypi/simple/
  3. https://pypi.mirrors.ustc.edu.cn/simple/
  4. http://pypi.hustunique.com/
  5. http://pypi.sdutlinux.org/
  6. http://pypi.douban.com/simple/
复制代码
1.2 安装wheel
  1. pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码
1.3 安装twine
  1. pip install twine -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码
1.4 注册PyPI账号

此网址注册一个即可
2.编写setup.py

2.1 项目文件树


  • 你的项目可能是这样的...
  1. xu736946693@ubuntu:~/Desktop/python-template$ tree
  2. .
  3. ├── bin
  4. │   └── start.py
  5. ├── conf
  6. │   └── yourfile.conf
  7. ├── dataBase
  8. │   └── yourDB
  9. ├── docs
  10. │   └── introduction.md
  11. ├── lib
  12. │   └── yourlib.py
  13. ├── LICENSE
  14. ├── log
  15. │   └── version.md
  16. ├── package_name
  17. │   ├── __init__.py
  18. │   └── module1
  19. │       └── __init__.py
  20. ├── README.md
  21. ├── res
  22. │   ├── READMEimgRes
  23. │   │   ├── 7ac23192b1904eb790272d8462cec5b8.png
  24. │   │   └── d919d615def3466f9ff73488c4e62aac.png
  25. │   └── yourResource
  26. │       └── resourceFile
  27. ├── settings.zip
  28. ├── setup.py
  29. └── tests
  30.     └── test.py
  31. 12 directories, 16 files
复制代码
2.2 编写setup.py文件

setup.py文件是用来打包和上传你的包的重要文件,它有固定的编写范式。下面我将给出我的demo并附上详细注释。
  1. from setuptools import setup, find_packages
  2. from os import path
  3. this_directory = path.abspath(path.dirname(__file__))
  4. with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
  5.     my_long_description = f.read()
  6. setup(
  7.     # 关于classifiers的描述详见如下
  8.     # https://pypi.org/search/?q=&o=&c=Topic+%3A%3A+Software+Development+%3A%3A+Build+Tools
  9.     classifiers=[
  10.         # 属于什么类型
  11.         "Topic :: Software Development :: Libraries :: Python Modules",
  12.         # 发展时期,常见的如下
  13.         # Development Status:: 1 - Planning
  14.         # Development Status:: 2 - Pre - Alpha
  15.         # Development Status:: 3 - Alpha
  16.         # Development Status:: 4 - Beta
  17.         # Development Status:: 5 - Production / Stable
  18.         # Development Status:: 6 - Mature
  19.         # Development Status:: 7 - Inactive
  20.         "Development Status :: 4 - Beta",
  21.         # 许可证信息
  22.         "License :: OSI Approved :: MIT License",
  23.         # 目标编程语言
  24.         # Programming Language :: C
  25.         # Programming Language :: C++
  26.         # Programming Language :: Python :: 3.4
  27.         # Programming Language :: Python :: 3.5
  28.         # Programming Language :: Python :: 3.6
  29.         # Programming Language :: Python :: 3.7
  30.         # Programming Language :: Python :: 3.8
  31.         # Programming Language :: Python :: 3.9
  32.         "Programming Language :: Python :: 3",
  33.         "Programming Language :: Python :: 3.3",
  34.         "Programming Language :: Python :: 3.4",
  35.         "Programming Language :: Python :: 3.5",
  36.         "Programming Language :: Python :: 3.6",
  37.         "Programming Language :: Python :: 3.7",
  38.         "Programming Language :: Python :: 3.8",
  39.         "Programming Language :: Python :: 3.9",
  40.         # 运行的操作系统
  41.         # "Operating System :: POSIX :: Linux",
  42.         "Operating System :: Microsoft :: Windows",
  43.         # 运行的环境
  44.         # "Environment :: GPU :: NVIDIA CUDA :: 12",
  45.         # 开发的目标用户
  46.         # Intended Audience :: Customer Service
  47.         # Intended Audience :: Developers
  48.         # Intended Audience :: Education
  49.         # ...
  50.         # Intended Audience :: End Users/Desktop
  51.         # Intended Audience :: Financial and Insurance Industry
  52.         # Intended Audience :: Healthcare Industry
  53.         "Intended Audience :: End Users/Desktop",
  54.         # 自然语言
  55.         "Natural Language :: English",
  56.         "Natural Language :: Chinese (Simplified)",
  57.     ],
  58.     # 如果上传时出现ERROR:The user '' isn't allowed to upload to project '',换个名字,长一点无所谓,不能跟别人重复
  59.     name="projectTemplate",
  60.     version="1.0.0",
  61.     author="Han Xu",
  62.     author_email="736946693@qq.com",
  63.     description="This is a project template.",
  64.     long_description=my_long_description,
  65.     # 存放源码的地址,填入gitee的源码网址即可
  66.     # url="https://gitee.com/UnderTurrets/",
  67.     packages=find_packages(),
  68.     # README.md文本的格式,如果希望使用markdown语言就需要下面这句话
  69.     long_description_content_type="text/markdown",
  70.     # 安装过程中,需要安装的静态文件,如配置文件、service文件、图片等
  71.     # data_files=[
  72.     #      ("", ["conf/*.conf"]),
  73.     #      ("/usr/lib/systemd/system", ["bin/*.service"]),
  74.     #            ],
  75.     # 希望被打包的文件
  76.     # package_data={
  77.     #     "":["*.txt"],
  78.     #     "bandwidth_reporter":["*.txt"]
  79.     #            },
  80.     # 不打包某些文件
  81.     # exclude_package_data={
  82.     #     "bandwidth_reporter":["*.txt"]
  83.     #            },
  84.     # 表明当前模块依赖哪些包,若环境中没有,则会从pypi中下载安装
  85.     # install_requires=["requests",],
  86.     # setup.py 本身要依赖的包,这通常是为一些setuptools的插件准备的配置
  87.     # 这里列出的包,不会自动安装。
  88.     # setup_requires=["",],
  89.     # 仅在测试时需要使用的依赖,在正常发布的代码中是没有用的。
  90.     # 在执行python setup.py test时,可以自动安装这三个库,确保测试的正常运行。
  91.     # tests_require=[
  92.     #     "",
  93.     # ],
  94.     # install_requires 在安装模块时会自动安装依赖包
  95.     # 而 extras_require 不会,这里仅表示该模块会依赖这些包
  96.     # 但是这些包通常不会使用到,只有当你深度使用模块时,才会用到,这里需要你手动安装
  97.     # extras_require={
  98.     #     "":  [""],
  99.     # },
  100. )
复制代码

  • 根据自己的需要更改即可
3.构建


  • 在项目空间下执行如下指令:
  1. python setup.py sdist bdist_wheel
复制代码

  • 可以看到项目下会自动生成build目录和lib目录等:
  1. xu736946693@ubuntu:~/Desktop/python-template$ tree -L 3
  2. .
  3. ├── bin
  4. │   └── start.py
  5. ├── build
  6. │   ├── bdist.linux-x86_64
  7. │   └── lib
  8. │       └── package_name
  9. ├── conf
  10. │   └── yourfile.conf
  11. ├── dataBase
  12. │   └── yourDB
  13. ├── dist
  14. │   ├── projectTemplate-1.0.0-py3-none-any.whl
  15. │   └── projectTemplate-1.0.0.tar.gz
  16. ├── docs
  17. │   └── introduction.md
  18. ├── lib
  19. │   └── yourlib.py
  20. ├── LICENSE
  21. ├── log
  22. │   └── version.md
  23. ├── package_name
  24. │   ├── __init__.py
  25. │   └── module1
  26. │       └── __init__.py
  27. ├── projectTemplate.egg-info
  28. │   ├── dependency_links.txt
  29. │   ├── PKG-INFO
  30. │   ├── SOURCES.txt
  31. │   └── top_level.txt
  32. ├── README.md
  33. ├── res
  34. │   ├── READMEimgRes
  35. │   │   ├── 7ac23192b1904eb790272d8462cec5b8.png
  36. │   │   └── d919d615def3466f9ff73488c4e62aac.png
  37. │   └── yourResource
  38. │       └── resourceFile
  39. ├── settings.zip
  40. ├── setup.py
  41. └── tests
  42.     └── test.py
复制代码
4.上传


  • 在项目空间下执行如下指令:
  1. twine upload -u <the usrname of your PyPI account> -p <the password of your PyPI account> dist/<the files you want to upload>
复制代码

  • 如果你更新了代码,记得更新setup.py中的版本号,重新构建你的代码,再次上传就好了。
ERROR:The user 'XXX' isn't allowed to upload to project ''


  • 你的软件包名字是PyPI用以区分的唯一标识,因此必须全球唯一
如果上传时出现ERROR:The user 'XXX' isn't allowed to upload to project '',换个名字,长一点无所谓,不能跟别人重复。
2024.1.19更新:

目前PyPI官方强制要求两步验证,同时关闭了在终端中输入账密上传包的方式。目前需要在账户中设置API才可以上传。

  • 设置API


  • 在家目录建立.pypirc文件。对于windows用户,即'C:\Users\\.pypirc。对于Linux用户,即'~\.pypirc。
  • twine上传
  1. twine upload dist/*
复制代码
本文由博客一文多发平台 OpenWrite 发布!

来源:https://www.cnblogs.com/UnderTurrets/p/18377676
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具