Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

preface

I don’t know if you have noticed that when we define a module and want to import the custom module in another module, the two modules must be in the same project or in the same path to import, otherwise we will report module undefined error. However, when using built-in modules such as OS or installed third-party repositories for requests, they can be imported wherever they are, regardless of whether they are in the same project or in the same path. Why is this? If we customize a universal module that can be used in many different projects, can it be used anywhere like the built-in system?

Module construction

If we have a set of functional modules that connect different database operations, and we want to apply these modules to different projects, the dumbest way is to copy them for each project and use them again, which is obviously not the best way. Another neat way to do this is to publish, package, and install the modules we have written so that they can be imported and used directly. Here is a brief description of the module publishing steps

  1. The directory structure of the database operation service is as follows:
.
|--dbserver
       |--sqlserver_help.py
       |--mysql_help.py
       |--oracle_help.py
       |--__init__.py
Copy the code
  1. Create a new setup.py file in the dbServer sibling directory and add the following code:
from distutils.core import setup

setup(name='dbserverhelper',version='1.0',description='database server helper',autor='Alvin',py_modules=['dbserver.sqlserver_help'.'dbserver.mysql_help'.'dbserver.oracle_help'])
Copy the code

The parameters of the setup function are as follows:

Name: the published name of the module, which can be defined arbitrarily

Version: indicates the released version of the module

Description: indicates the description of the module

Author: indicates the author of a module

Py_modules: This parameter is a list of what py files are required for the module to be packaged and distributed. The format is package name. module name, without suffix

  1. Building blocks

Open the command line to switch to the setup.py directory and execute the following statement

python setup.py build

The directory to build must contain the __init__.py file, which means that the directory to package must be a Python package, otherwise the build will not be possible.
Copy the code

Built directory structure:

.
|--build
|    |--lib
|    |    |--dbserver
|    |    |    |--sqlserver_help.py
|    |    |    |--mysql_help.py
|    |    |    |--oracle_help.py
|    |    |    |--__init__.py
|
|--dbserver
|      |--sqlserver_help.py
|      |--mysql_help.py
|      |--oracle_help.py
|      |--__init__.py
|
|--setup.py
Copy the code
  1. Generate and publish compressed package \

Execute the following statement in the same directory as setup.py:

python setup.py sdist
Copy the code

After this command is executed, a compressed package dbServerhelp-1.0.tar. gz is generated. The latest directory structure is as follows:

. |--build | |--lib | | |--dbserver | | | |--sqlserver_help.py | | | |--mysql_help.py | | | |--oracle_help.py | | | | - set p y | - dist | | - dbserverhelp - 1.0, tar, gz | | - MANIFEST | | - dbserver | | -- sqlserver_help. Py | | -- mysql_help. Py  | |--oracle_help.py | |--__init__.py | |--setup.pyCopy the code

After the above four steps are complete, the custom module is built and packaged.

Module installation

To install our module, first find the dbServerhelp-1.0.tar. gz package generated in the above steps and unzip it to any location, then go to the folder behind the antidote to open CMD and run the following command:

python setup.py install

Note: If you need to specify the installation directory during install, you can use:Python setup.py install --prefix= Installation pathCopy the code

Module import

You can import a module through import XXX or import XXX from XXX.

In this way, custom modules can be packaged and installed and used.