欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

What is module and How to publish module in Python?

程序员文章站 2022-05-07 13:40:19
...
  • Overview

    理解module, library, package in Python

    In Python, a module is a self-contained file with Python statements and definitions, file.py can be considered as a module named file.

    “PyPI” should be pronounced like “pie pea eye”, ***Python Package Index***, is also known as the Cheese Shop. It is the official third-party software repository for Python, just like CPAN is the repository for Perl. Some package manager’s such as pip, use PyPI as the default source for packages and their dependencies.

  • python -m

    The python interpreter program has an -m option that allows you to specify a module name instead of a file name.

    $ python hello.py
    # is equal to 
    $ python -m hello
    

    One advantage of the latter is that it allows you to call modules that are built into Python as well.

  • Publish an Open-Source Python Package to PyPI

    1. Naming Your Package

      All packages on PyPI need to have unique names. Use the PyPI search to check if a name is already taken.

      You can use different names for your package on PyPI and when importing.

    2. Configuring Your Package

      理解setup.py in Python

      In order for your package to be uploaded to PyPI, you need to provide some basic information about it. This information is typically provided in the form of a setup.py file.

      The setup.py file should be placed in the top folder of your package.

      The parameters that are 100% necessary in the call to setup() are name/version/packages.

    3. Documenting your package

      深入理解document & comment in Python

      README or website or something else.

    4. Versioning your package

      If you want to update your package on PyPI, you need to increase the version number first.

      You may need to specify the version in different files inside your project, you can use a tool called Bumpversion to keep consitent.

    5. Adding files to your package

      Adding files not source code files. include_package_data

    6. Prepare for Publishing to PyPI

      Resigter your account on PyPI and TestPyPI.

      To upload your package to PyPI, you’ll use a tool called Twine.

    7. Building your package

      理解Python wheels

      Packages on PyPI are not distributed as plain source code. Instead, they are wrapped into distribution packages. The most common formats for distribution packages are source archives and Python wheels.

      $ python setup.py sdist bdist_wheel # to create a source archive and a wheel
      
    8. Testing your package
    9. Uploading your package
      $ twine upload --repository-url https://test.pypi.org/legacy/ dist/*
      
  • References

  1. Docs.ThePythonTutorial.Modules
  2. How to Publish an Open-Source Python Package to PyPI
  3. What is PyPI & How To Publish An Open-Source Python Package to PyPI
  4. PEP 518 – Specifying Minimum Build System Requirements for Python Projects