English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Publishing Your Python Module

When we learn Python, in addition to installing some modules with pip, sometimes we download and install packages from websites. I also want to make my self-written modules into such packages. How should I do it, and how to publish them?

It usually takes the following four steps:

1First, create a folder for the module.

Let's take a simple example. You have written an 'add.py' module file with an 'add' method that implements addition. This first step requires you to create a folder. And copy 'add.py' into this folder. For simplicity, let's name the folder 'add'

add
|__add.py

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

Edit this file and add the following code. This file contains metadata related to the release, as shown in the following example. The specific metadata may differ from the example:

from distutils.core import setup
setup(
    name    = 'add',
    version   = '1.0.0',
    py_modules = ['add'],
    author   = 'huilan',
    author_email= '[email protected]',
    url     = 'http://www.lalalala.com',
    descriptioin= 'add two numbers',
  )

3Build a release file.

Now we have a folder that contains two files: the module code is placed in add.py, and the relevant metadata is placed in setup.py. Next, we will use Python's built-in publishing tools to create the release file.
Open a terminal in the add folder or use the cmd command line to cd to the add folder and execute the following command:

python3 setup.py sdist

4.Install the published module to your local Python.

Still in the terminal that was just opened, enter the following command:

sudo python3 setup.py install

Look at the release information that appeared on the screen, confirm that the installation was successful, and the release is ready.

Finally, we get the folder structure as follows:

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

Among them:

- The MANIFEST file contains the list of files to be released
- build\lib\add.py and add.py under the root directory are both code files
- dist\add-1.0.0.tar.gz is the release package
- add.pyc is the compiled version of the code
- setup.py stores metadata

 That's the material preparation for publishing your Python module. We will continue to supplement relevant information, thank you all for your support to this site!

Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)