SoFunction
Updated on 2025-03-02

Example analysis of Python module production method

This article describes the method of making Python modules. Share it for your reference, as follows:

1 Purpose

Use the installation of the framework into the python environment and call it as a third-party module.

2 Step 1: Completed writing

The following code is equivalent to a template. Just change the name field and change it to the corresponding module name that needs to be installed. For example, here is: py_plus

Put the file in the same directory as py_plus

from  import dirname, join
# from  import parse_requirements
from setuptools import (
  find_packages,
  setup,
)
def parse_requirements(filename):
  """ load requirements from a pip requirements file """
  lineiter = (() for line in open(filename))
  return [line for line in lineiter if line and not ("#")]
with open(join(dirname(__file__), './'), 'rb') as f:
  version = ().decode('ascii').strip()
setup(
  name='py-plus', # Module name  version=version,
  description='A mini web framework', # describe  packages=find_packages(exclude=[]),
  author='xx',
  author_email='your@',
  license='Apache License v2',
  package_data={'': ['*.*']},
  url='#',
  install_requires=parse_requirements(""), # The required operating environment  zip_safe=False,
  classifiers=[
    'Programming Language :: Python',
    'Operating System :: Microsoft :: Windows',
    'Operating System :: Unix',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6',
  ],
)

Note: There may be an error in the above code that requires additional installation of package modules and update setuptools

  • pip install packaging
  • pip install --upgrade setuptools

It may not exist, the corresponding one can be:

def parse_requirements(filename):
  """ load requirements from a pip requirements file """
  lineiter = (() for line in open(filename))
  return [line for line in lineiter if line and not ("#")]

3 Step 2: Completed writing

Function:

  • State the modules and versions supported by the dependent environment

use:

  • Use in
  • Placed in a directory of the same level
requests>=2.18.4
six>=1.11.0

4 Step 3: Completed writing

Function:

  • Indicate the current version, a qualified module must have the corresponding version number

use:

  • Use in
  • Placed in a directory of the same level

1.0

5 Step 4: Execute the installation command

step:

  1. Switch to the corresponding python virtual environment
  2. Switch to the directory
  3. Execute in terminalpython install

For more information about Python-related content, please check out the topic of this site:Summary of Python file and directory operation skills》、《Summary of Python text file operation skills》、《Python data structure and algorithm tutorial》、《Summary of Python function usage tips》、《Summary of Python string operation skills"and"Python introduction and advanced classic tutorials

I hope this article will be helpful to everyone's Python programming.