The release and installation of modules in Python

Source: Internet
Author: User

Modules (module)

Python has a concept called module, which is similar to a header file in C and a package in Java, such as in Python to invoke the SQRT function, which must be introduced into the math module with the Import keyword. In layman's terms, a module is like a toolkit, and if you want to use the tool in the toolkit (like a function), you need to import the module.

Import Module

    • Import: Introduce a specific module, you can introduce multiple modules at once, separated by commas can

Eg:import modlue1,module2,module3,..... Modulen, when using the function in the module, the format is: Modulename.funname ()

    • From ...: Import a specified part from a module into the current namespace

Eg: from moddulename import name1 [, Name2 [, Name3 [,.... Namen]]

    • From ... import *: All contents of a module are imported into the current namespace

Eg: from modulename import *

Package

The package organizes the associated modules together, effectively avoiding the problem of module name conflict, and makes the application organization structure clearer.

A package can contain multiple modules.

Let's assume that our package example has the following directory structure:

Phone/    __init__.py    common_util.py    Voicedta/        __init__.py        Pots.py        Isdn.py    Fax/        __init__.py        G3.py    Mobile/        __init__.py        Analog.py        igital.py    Pager/        __init__.py        Numeric.py

The Phone is the topmost package, VOICEDTA, etc. is its child package. We can import the child package like this:

import Phone.Mobile.AnalogPhone.Mobile.Analog.dial()

You can also use From-import to implement different requirements of the import

Module production

The function method file is defined first, and several function methods are defined in the file to be available to the outside world.

There is usually a file in the Moudle __init__.py . Some __init__.py of them are blank, others have __all__ parameters.

If the other page __init__.py is blank when import, you can import all the functions directly into Moudle. If it is __init__.py defined __all__ , import only imports some of the __all__ defined content.

Module release

    • Directory structure of the module:
├── setup.py├── suba│   ├── aa.py│   ├── bb.py│   └── __init__.py└── subb    ├── cc.py    ├── dd.py    └── __init__.py

The directory structure of the module contains a setup.py file that defines module information such as the module name, the included module, and so on.

    • Content in setup.py:
 fromDistutils.coreImportSetupsetup (Name="Module Name", version="version", description="Module Description", author="Module Author", py_modules=['Suba.aa','suba.bb','subb.cc','subb.dd' (here is mainly the contents of the module included)])
    • 构建模块

    Linux命令(当前位置位于模块文件夹):
python setup.py build
构建后目录结构.├── build│   └── lib.linux-i686-2.7│       ├── suba│       │   ├── aa.py│       │   ├── bb.py│       │   └── __init__.py│       └── subb│           ├── cc.py│           ├── dd.py│           └── __init__.py├── setup.py├── suba│   ├── aa.py│   ├── bb.py│   └── __init__.py└── subb    ├── cc.py    ├── dd.py    └── __init__.py
    • 生成发布压缩包

Linux命令(当前位置位于模块文件夹):
 python setup.py sdist打包后,生成最终发布压缩包 moduleName-version.tar.gz , 目录结构.├── build│   └── lib.linux-i686-2.7│       ├── suba│       │   ├── aa.py│       │   ├── bb.py│       │   └── __init__.py│       └── subb│           ├── cc.py│           ├── dd.py│           └── __init__.py├── dist│   └── xwp-1.0.tar.gz├── MANIFEST├── setup.py├── suba│   ├── aa.py│   ├── bb.py│   └── __init__.py└── subb    ├── cc.py    ├── dd.py    └── __init__.py

模块安装

    • Find the module's compression package
    • Extract
    • Go to Folder
    • Execute command sudo python setup.py install

At this point, the module has been installed in the system, you can import the module directly using the Import keyword.

PS: If you perform a directory installation at install time, you can use thepython setup.py install --prefix=安装路径

The release and installation of modules in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.