Publish your Python module in detail

  • 2020-05-10 18:26:07
  • OfStack

When we were learning Python, in addition to installing some modules with pip, we sometimes downloaded and installed them from the website. I also wanted to make the modules I wrote into such an installation package. How should I do and how should I release them?

The following four steps are required:

1. First create a folder for the module.

For a simple chestnut, you write an add.py module file, which has an add method to implement addition. This first step requires you to create a folder. Copy add.py into this folder. For simplicity, let's call the folder add

add
|__add.py

2. Then create a file named "setup.py" in the new folder.

Edit the file and add the following code. This file contains metadata about publishing, which can differ from the following example:


from distutils.core import setup

setup(
    name    = 'add',
    version   = '1.0.0',
    py_modules = ['add'],
    author   = 'huilan',
    author_email= 'womende218@126.com',
    url     = 'http://www.lalalala.com',
    descriptioin= 'add two numbers',
  )

3. Build a release file.

We now have a folder containing two files: the module code in add.py, and the relevant metadata in setup.py. Next, we'll make a release file using Python's own publishing tool.
Open a terminal in the add folder, or the cmd command line cd into the add folder, and execute the following command:

python3 setup.py sdist

Install the release module into your local Python.

Still in the terminal you just opened, enter the following command:

sudo python3 setup.py install

See the release information on the screen, confirm the installation is successful, and the release is ready.

Finally, the folder structure we get is as follows:

add
    |__ MANIFEST
    |__ build
    |                 |__ lib
    |                               |__ add.py
    |__ dist
    |               |__ add-1.0.0.tar.gz
    |__ add.py
    |__ add.pyc
    |__ setup.py

Among them:

This file contains the list of files in the publication
-build \lib\ add.py and add.py in the root directory are both code files
-dist \ add-1.0.0.tar.gz is a distribution package
-add.pyc is compiled version code
- setup.py stores metadata

  above is to release your Python module information collation, continue to add the relevant information, thank you for your support!


Related articles: