Home  >  Article  >  Backend Development  >  Detailed explanation of publishing your Python module

Detailed explanation of publishing your Python module

高洛峰
高洛峰Original
2017-02-22 17:13:22985browse

When we are learning Python, in addition to using pip to install some modules, we sometimes download installation packages from the website and install them. I also want to make the modules I wrote into such an installation package. What should I do? , how to publish it?

About the following four steps are required:

1. First create a folder for the module.

As a simple example, you write an add.py module file, and there is an add method in it to implement addition. This first step requires you to create a folder. And copy add.py to this folder. For simplicity, we name the folder add

add
|__add.py

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

Edit this file and add the following code. This file contains metadata about the publication, as in the example below. The specific metadata can be different from the 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 Publish files.

Now we have a folder containing two files: the module code is placed in add.py, and the relevant metadata is placed in setup.py. Next, we will use the publishing tool that comes with Python to create the publishing file.
Open a terminal in the add folder, or cd the cmd command line to the add folder, and execute the following command:

python3 setup.py sdist

4. The release module is installed into your local Python.

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

sudo python3 setup.py install

See the release information appears on the screen to confirm that the installation is successful. , the release is ready.

The final folder structure we got is as follows:

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

Among them:


- The MANIFEST file contains the list of files being published

- build\lib\add.py and add.py in the root directory are both code files
- dist\add-1.0.0.tar.gz is the release package
- add.pyc is the compiled version code
- setup.py stores metadata

The above is to publish your Python module We will organize the information and continue to add relevant information in the future. Thank you for your support of this site!

For more detailed articles on publishing your Python module, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn