Python project package upload pypi

1. Overview

This blog post will briefly introduce how to package and publish your own python code to pypi, so as to realize "pip install {your package name}".

2. Create a package file

Insert picture description here

3. Create setup.py

setup.py is the build script of setuptools. It tells setuptools your package (such as name and version) and the code files to include.

import setuptools

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

setuptools.setup(
    name="gdalTools",
    version="0.1",
    author="******",
    author_email="*******@qq.com",
    description="some useful tools in gdal",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/******/******.git",
    packages=setuptools.find_packages(),
    classifiers=(
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ),
)

  • name is the distribution name of the package;
  • version is the package version;
  • author is used to identify the author of the package;
  • description is a short, one-sentence package summary;
  • url is the URL of the project homepage. For many projects, this is just a link to GitHub, GitLab, Bitbucket or similar code hosting services ;
  • packages is a list of all imported Python packages that should be included in the distribution package. We can use automatic discovery of all packages and sub-packages instead of manually listing each package. In this case, the package list will be example_pkg because it is the only package that exists;
  • Classifiers tell the index and click on some other metadata about your package. In this case, the package is only
    compatible with Python 3, licensed under the MIT license, and has nothing to do with the operating system. You should always include at least the Python version used by your package, the licenses available for the package, and the operating system your package will use.

4. Create README.md

Open README.md and enter the following. You can customize this item and put some introduction to the item.

5. Create License

Copyright (c) 2005-2020, NumPy Developers.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
       copyright notice, this list of conditions and the following
       disclaimer in the documentation and/or other materials provided
       with the distribution.

    * Neither the name of the NumPy Developers nor the names of any
       contributors may be used to endorse or promote products derived
       from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

6. Generate distribution files

The next step is to generate a distribution package for the package. These are the files uploaded to the package index and can be installed via pip.
Now run this command from the same directory where setup.py is located:

python setup.py sdist bdist_wheel

Insert picture description here
This command should output a lot of text, and once completed, two files should be generated in the dist directory:
Insert picture description here

7. Upload PYPI

The first thing you need to do is to register an account on PyPI. Test PyPI is a separate instance of the package index for testing and experimentation. This is great for things like this tutorial where we don't necessarily want to upload to the real index. To register an account, please visit https://pypi.org/
and enter at the command line of the project:

twine upload dist/*

Enter the username and password registered with PyPI. After the command completes, you should see output similar to this:
Insert picture description here

8. Results inspection

Insert picture description here

reference

https://zhuanlan.zhihu.com/p/61174349

Guess you like

Origin blog.csdn.net/weixin_42990464/article/details/111414378