10 minutes to make open source pip package

Posted by 5kyy8lu3 on Tue, 08 Feb 2022 06:32:56 +0100

1, Understanding PyPI

It can be understood that it is a package management platform. All packages we commonly use will be hosted on this platform. Through pip install xx, we will find this package in PyPI and download it to our computer.

2, Process analysis

For module developers, there are essentially three things to do:

  • Open the refrigerator door
  • Put the elephant in
  • Close the refrigerator door

For module developers, there are essentially three things to do:

  • Write module
  • Package modules
  • Upload to PyPI (PyPI account needs to be registered first)
    • Register PyPI account
    • Install upload tool
    • Upload based on tools

 

For the user of the module, only two things need to be done:

  • Install the module through # pip install #
  • Call module

 

3, Making open source modules

Now, suppose we make a module called fucker.

Step 1 project folder

The required directory structure is as follows:

fucker
├── LICENSE           # Declaration, to show the module users, to put it bluntly, is whether the users can be used for commercial purposes free of charge.
├── README.md         # Module introduction
├── demos             # Use case
├── fucker            # Module code directory
│   └── __init__.py
└── setup.py          # Script that provides information to setuptools (name, version, etc.)

 

1.1 LICENSE

The LICENSE file is the LICENSE of our module. For module users, it means whether users can use it for commercial purposes free of charge. Generally, open source software will choose a relatively broad LICENSE MIT, that is, the author retains the copyright without any other restrictions.

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.

For more additional licenses, see: Choose an open source license | Choose a License

1.2 README

readme is the description information of the current module, which is generally written in markdown format, such as:

fucker It's a tool for hanging and exploding the sky...

This file can also write a simple user manual for the module. If the manual is too complex, it is recommended to create a doc folder to store the user manual.

 

1.3 demos directory

demos will generally write some examples of the module, so that users can quickly apply the module to generation.

 

1.4 setup.py

setup.py file is actually a configuration file, which is used to provide setuptools with some module related information, such as module name, module version, applicable python version, author, github address, etc.

import setuptools
with open("README.md", "r") as fh:
    long_description = fh.read()
setuptools.setup(
    name="fucker",  # Module name
    version="1.0",  # current version
    author="wupeiqi",  # author
    author_email="wupeiqi@live.com",  # Author email
    description="A very NB Wrapped",  # Module introduction
    long_description=long_description,  # Module details
    long_description_content_type="text/markdown",  # Module details format
    # url="https://github.com/wupeiqi/fucker ", # module GitHub address
    packages=setuptools.find_packages(),  # Automatically find imported modules in the project
    # Module related metadata
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    # Dependent module
    install_requires=[
        'pillow',
    ],
    python_requires='>=3',
)

Note: setuptools is a package management tool that can be used to package and install modules.

1.5 fucker directory

For plug-in content, you can write relevant code here, such as WP py

def func():
    print("A magical bag")

The final folder is as follows:

fucker
├── LICENSE
├── README.md
├── demos
├── fucker
│   ├── __init__.py
│   └── wp.py
└── setup.py

 

 

Step 2: Code packaging & uploading

After the folder and code are written in the above steps, the code needs to be packaged.

2.1 installation of packaging tools (there is no need to repeat the installation)

To package the code, you need to install setuptools and wheel first, which can be installed separately or pip, so that the two tools can be installed automatically.

Note: for installed users, if you want to update setuptools and wheel, you can use the following command:

python -m pip install --upgrade setuptools wheel

 

2.2 packaging code

Then input at the Terminal terminal of pychart:

python setup.py sdist bdist_wheel

The following files are generated:

fucker
├── LICENSE
├── README.md
├── fucker
│   ├── __init__.py
│   └── wp.py
├── fucker.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
├── build
│   ├── bdist.macosx-10.6-intel
│   └── lib
│       └── fucker
│           ├── __init__.py
│           └── wp.py
├── demos
├── dist
│   ├── fucker-0.0.1-py3-none-any.whl
│   └── fucker-0.0.1.tar.gz
└── setup.py

2.3 release module (upload)

After the file is packaged, you need to upload the packaged file to PyPI. If you want to upload it, you need to go first PyPI · The Python Package Index Register an account.

  • Install the tool used to release the module: Twin [installed without repeated installation]

python -m pip install --upgrade twine
 or
pip install --upgrade twine
# Tip: python -m is used to run library module as a script (terminates option list)
  • release
python -m twine upload --repository-url https://upload.pypi.org/legacy/  dist/*
or
twine upload --repository-url https://upload.pypi.org/legacy/  dist/*

When uploading, you are prompted to enter the user name and password of PyPI

 

Step 3 installation and use

3.1 installation

  1. pip install module

3.2 application

from fucker import wp
wp.func()

Topics: Python pip Pycharm