Python Base---Function Module 5 (modules and packages)

Source: Internet
Author: User

Modules and Packages

First, the module

1, how the module was born.

In the development of computer programs, as the program code more and more, in a file code will be more and more long, more and more difficult to maintain. In order to write maintainable code, we grouped many functions into separate files so that each file contained relatively few code, and many programming languages used this way of organizing code. In Python, a. py file is called a module.

2, the role of the module.

Improves the maintainability of the code. The second is that writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties. In addition, the use of modules can also avoid conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name.

3, the classification of the module

1. System built-in module, Python standard library

2. Third-party module, that is, others have prepared the module, download it directly to use it (scary,requests, etc.)

3. Application customization module, which is a module written by the programmer himself.

4. How to import the module.

1.import statements

Format: Import module name

Note: When the module is imported, if the built-in module is imported directly, if it is a third-party module or a custom module, it will be searched according to the path given by Sys.path.

A module can be imported multiple times at its current location, but only the first import executes code within the source file because the first import loads the contents of the module into

Memory, and subsequent imports at the current file location are pointing to the existing modules in memory

2.from...import Statements

Format: Import module name import variable name

Note: This declaration does not import the entire ModuleName module into the current namespace, only the list of variables inside it is introduced into the global symbol table of the module that executes the declaration.

3. Supplement.

When a module is import, a new namespace is first created to hold the names of all the variables defined in the module, and all the code within the foo.py is executed within that namespace.

The last thing you need to know is that the Import keyword defines a name, but at the moment we use import to define a module name Foo, which is the namespace of the foo.py, and the way foo.

is to find the name from the namespace, you can use foo.__dict__ to see the names.

Comment: Two procedures for import: Create a new Namespace {Name: Object and} execute the called module (the second time will not be executed).

Second, the package

1, why there will be a package.

What if a different person writes the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages, after the package is introduced, as long as the top-level package

Name does not conflict with others, all modules will not conflict with others.

2. What is a package?

The role of the package is the same as the folder, but the difference with the folder is that a new package will automatically generate a __init__.py file, the file can write content or do not write content. __init__.py's

The module name is dead corresponding to the package name, meaning to die when you call this package is actually called the package under the init of the thing.

3. How to call in-package module

1. If the calling module and the called module are in the same package, the two modules belong to the brother relationship then the two modules can be called directly.

2. If the calling module and the called module are within the same package, the two modules belong to the pro-nephew relationship, then the module must be called with the From Package name import module name.

3. if the calling module and the called module are in the same package, the two modules belong to the table nephew relationship, so long need to call the OS module, the path of the called package is added to the Sys.path.

 eg: import os, sys #调用模块
Base_dir=os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) #调用文件路径
Print (Base_dir)
Sys.path.insert (0, Base_dir) #添加到搜索路径

Python Base---Function Module 5 (modules and packages)

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.