Introduction and use of modules in Python

Source: Internet
Author: User

There is a concept in Python called module, which is similar to a header file in C and a package in Java, such as calling the SQRT function in Python, which must be introduced into the math module with the Import keyword. Here's a look at the modules in Python.

The popular point: The module is like a toolkit, to use the tool in the toolkit (like a function), you need to import this module

1.import

Using the keyword import in Python to introduce a module, such as referencing module math, can be introduced with import math at the very beginning of the file.

Shaped like:

Importmodule1,mudule2 ...

When the interpreter encounters an import statement, the module is imported if it is in the current search path.

When you call a function in the math module, you must refer to this:

Module name. function name

This method must be called with the module name, because there may be cases where a function with the same name in multiple modules is called only by the function name, and the interpreter cannot know exactly which function to invoke. So if the module is introduced as described above, the calling function must add the module name.

Sometimes we just need to use a function in the module, just to introduce the function, which can be implemented in the following way:

From Module name Import function name 1, function name 2 ....

Not only can we introduce functions, but we can also introduce some global variables, classes, etc.

Attention:

When introduced in this way, only the function name can be given when the function is called, and the module name cannot be given, but when the two modules contain the same name function, the subsequent introduction overwrites the previous one. In other words, if the function function () is in module A, there is also functional function () in module B, and if you introduce the functions in the first and B functions in a, then when you call function, is to execute the function functions in module B.

If you want to introduce everything in math at once, you can also use the From math import * to implement

2.from...import

The FROM statement of Python lets you import a specified part from the module into the current namespace

The syntax is as follows:

From ModName import name1[, name2[, ... Namen]]

For example, to import the Fibonacci function of a module FIB, use the following statement:

From fib import Fibonacci

Attention

• The entire FIB module will not be imported into the current namespace, it will only introduce the Fibonacci individual in the FIB

3.from ... import *

It is also possible to import all the contents of a module into the current namespace, just use the following declaration:

From ModName Import *

Attention

• This provides an easy way to import all the items in a module. However, such statements should not be used too much.

4.as

5. Positioning module

When you import a module, the Python parser's search order for the module location is:

1. Current directory

2. If it is not in the current directory, Python searches for each directory under the shell variable Pythonpath.

3. If none are found, Python will look at the default path. Under UNIX, the default path is typically/usr/local/lib/python/

4. The module search path is stored in the Sys.path variable of the system module. The variable contains the current directory, Pythonpath, and the default directory determined by the installation process.

6. Module making

1. Define your own module

In Python, each Python file can be used as a module whose name is the name of the file.

For example, there is a file test.py that defines the function add in test.py

2. Call the module of your own definition

In other files, you can import test first, then call through Test.add (A, B), and of course, through the From test import add to introduce

main.py

3. Test module

In practice, when a developer finishes writing a module, in order for the module to achieve the desired effect in the project, the developer will add some test information to the Py file itself, for example:

If you introduce this file in another py file, think about whether the code that you are testing will also be executed!

At this point, you can find the test code in test.py, should be executed when the test.py file is executed separately, should not be referenced in other files and executed

To solve this problem, Python has a variable when executing a file __name__

Summarize:

• Can be judged according to the results of the __name__ variable, is the direct execution of the Python script or is introduced to execute, so that can selectively execute the test code

7. __all__ in the module

1. No __all__

2. There are __all__ in the module

Packages in 8.python

1. Introduction of the Package

There are 2 modules that are functionally connected

So put it in the same folder

Use the import file. The way the module is imported

Importing from the From folder import module

Create a __init__.py file under the MSG folder

Write in the __init__.py file

Reuse the From folder import module as imported

Summarize:

The package will have the associated modules organized together, that is put into the same folder, and in this folder to create a name of the __init__.py file, then this folder is called the package

Effectively avoid the problem of module name conflict, make the application organization structure clearer

What is the 2__init__.py file for?

__init__.py controls the import behavior of the package

Import functions can be added within __init__ such as from AB import a

3__init__.py is empty

Simply import this package and not import the modules in the package

4__all__

In the __init__.py file, define a __all__ variable that controls the module imported from the Package name import *

5. (understanding) content can be written in the __init__.py file

You can write statements in this file that are executed when you import them.

__init__.py file

6. Extensions: Nested packages

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

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

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

The first approach is to import only the top-level child package, and then use the attribute/point operator to reference the sub-Baoshu:

In addition, we can also refer to more sub-packages:

In fact, you can always import along the sub-package's tree structure:

In our directory structure above, we can find a lot of __init__.py files. These are initialization modules that need to be used when the From-import statement imports a child package. If they are not used, they can be empty files.

The package also supports the From-import all statement:

However, such a statement will import which files depend on the filesystem of the operating system. So we add the __all__ variable to the __init__.py. The variable contains the name of the module that should be imported when executing such a statement. It consists of a list of module name strings.

Summarize:

9. Module release

The 1.mymodule directory structure is as follows:

2. Edit the setup.py file

Py_modules you need to indicate which py file you want to include

3. Building the module

Python setup.py Build

4. Generate a Release archive package

Python setup.py sdist

After packaging, build the final release archive package yongge-1.0.tar.gz, directory structure

10. Installation and use of the module

1. How to Install

1. Find the module's compression package

2. Unzip

3. Go to the folder

4. Execute command python setup.py install

Attention:

• If you perform a directory installation at install time, you can use the Python setup.py install--prefix= installation path

2. Introduction of Modules

In the program, use the from import to complete the installation of the module

From Module name Import module name or *

You are welcome to join the Learning Exchange Group if you encounter any problems or want to acquire learning resources in the learning process.

626062078, we learn python! together.

Introduction and use of modules in Python

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.