How to package and upload a Python program or module package to PyPi for others to use pip to download?

        Background: Since the author's company often needs to use various time-related data when doing automation, it is very troublesome to use the time module to convert each time. The author encapsulates some common time conversion methods by himself. Hey, guess what, after using it, everyone Everyone agreed, so I thought about how to make a python package for everyone to use together. After going through Baidu, I found the simplest and clearest process, so I recorded it for future use.

Install dependencies

whell Used for packaging and twine uploading packages to pypi.org

pip install whell   #一般python自带,不用下载
pip install twine

If you cannot download, please try to change the download source. You may also encounter the situation that the pip version is too low and needs to be upgraded. Try to install after the upgrade.

pip install wheel

Change to:

pip install wheel -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

Register a pypi account (you can ignore this step if you already have one)

Registration address: PyPI · The Python Package Index

Create package file

The project structure diagram that needs to be packaged is as follows:

/packaging_tutorial   #任意名称均可
  /example_pkg   # 需要上传的包名称,里面装的是你的代码
    xx.py    # 你的代码、函数或方法逻辑
    __init__.py
  setup.py   # 打包的配置文件
  LICENSE    # 里面放许可证
  README.md  # 项目简介

Take my project as an example:

         The time_util_wj directory is the project directory we want to package (this name must be the same as the project name in setup.py, and the name of pip install XXXX is the same). LICENSE It is an open source protocol of the software. If you don’t know how to choose, you can go to https://choosealicense.com  to choose it. readme.md It is the description of the software or package, and you can write some brief descriptions of the warehouse address. setup.py The packaged configuration file will be described in detail later.

setup.py packaging configuration file description

setup.pyis the build script for setuptools . It tells setuptools your package (e.g. name and version) and which code files to include.

Open it setup.pyand enter the following. You should update the package name to include your username (e.g. time_util_wjyou can personalize other values ​​if you wish)

setup.py :

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="time_util_wj",  # 项目名称,保证它的唯一性,不要跟pypi上已存在的包名冲突即可
    version="1.0.0",  # 程序版本
    py_modules=['time_utils'],  # 需要上传的模块名称,这样可以让这些模块直接import导入
    author="WangJie",  # 项目作者
    author_email="[email protected]",  # 作者邮件
    description="提供了各种时间转换的方法",  # 项目的一句话描述
    long_description=long_description,  # 加长版描述
    long_description_content_type="text/markdown",  # 描述使用Markdown
    url="https://github.com/wangjie-jason/time_utils",  # 项目地址
    packages=setuptools.find_packages(),  # 无需修改
    classifiers=[
        "Programming Language :: Python :: 3",  # 使用Python3
        "License :: OSI Approved :: Apache Software License",  # 开源协议
        "Operating System :: OS Independent",
    ],
)

At this point, all the preparations for packaging are ready, and the next step is to use commands to package and upload.

Pack

Open the terminal in the project root directory (that is, in the Automation directory):

python3 setup.py sdist bdist_wheel

After the packaging is complete, the project directory at this time is as follows:

dist The following is the package file to be uploaded.

Upload to http://pypi.org

The upload here can use test upload or direct upload.

The test upload will transfer the package to  https://test.pypi.org/ , so you need to register a test account under this address, and then use the following command to upload, which will not be introduced here.

python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

To upload directly to  pypi.org , use the command:

python3 -m twine upload dist/*

Then enter  pypi.org the registered account password to upload.

python3 -m twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: xxxxxx
Enter your password:

If there are no errors, the upload is successful! Log in to the website to see the package just uploaded

Then use  pip install XXXX the installer to test it.

other considerations

If you need to update the software package, remember to modify  setup.py the software version after modifying the code, so that it can be uploaded normally.

At this point, the upload of the Python package is done once, is it simple?

Guess you like

Origin blog.csdn.net/weixin_65784341/article/details/129860933