Python's modules and packages

Source: Internet
Author: User

One, Module 1. What is a module?

Common scenario: A module is a file that contains Python definitions and declarations, and the file name is the suffix of the module name plus the. Py.

In fact, the import loaded module is divided into four general categories:

1 code written using Python (. py file)

2 C or C + + extensions that have been compiled as shared libraries or DLLs

3 packages for a set of modules

4 built-in modules written and linked to the Python interpreter using C

2. Where do I use the module?

If you quit the Python interpreter and then re-enter, then the functions or variables you defined previously will be lost, so we usually write the program to a file so that it can be persisted and executed in Python test.py when needed, when test.py is called a scripting script.

With the development of the program, more and more functions, in order to facilitate management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. We can not only execute these files as scripts, but also import them into other modules as modules.

Modules can be used to improve the maintainability and reuse of code, as well as to avoid conflicting function names and variable names. Functions and variables of the same name can exist in separate modules, so when you write your own module, you do not have to consider that the name will conflict with other modules, but be careful not to conflict with the built-in function name as much as possible.

3. How to import modules 1. Direct Import Import Module  

Here's a major premise. The py executable file and module belong to the same directory (parent directory), such as:

    • main.py and Pwcong modules in Python directory
    • Execute file as main.py
    • The Pwcong folder is a single module

I wrote the function provided by the Pwcong module __init__.py , which only provides a hi function:

# Pwcong Module __init__.py#-*-coding:utf-8-*-def hi ():    print ("HI")

Execute file main.py Direct Import module:

# main.py#-*-coding:utf-8-*-import Pwcongpwcong.hi ()

Then we run the main.py can see the command Line window output a sentence hi , the first way to complete.

Use the module as: Import First-"then enter the module. Variables | functions, as in the example above Pwcong.hi ()

If you want to import multiple modules at the same time , you only need to delimit them with commas before the module name:

import module1,module2,module3.......

It is best to follow the following sequence when importing a module with an import statement:

1. Python Standard library module
2. Python third-party module
3. Custom Modules

 

2.import ... as ...

Import module format is as follows:

Import module path. File name as Alias

Release: When the file is under the current directory or the PATH environment variable, you can import the file name directly, otherwise you will start to specify the path from the project root directory. aliases, the direct introduction of filenames can be used without aliases, if a module path introduced with the best alias, the use of direct "Alias." Use

Second, from ... import ...

From ... import ... This introduction uses a point number to identify the exact location of the introduced class library.

Format:

Import module path. File name import variable name \ function \ class name

From module import XX

fromimport xx as rename

From module Import *
Import all names in My_module that are not preceded by an underscore (_) to the current location, and in most cases our Python program should not use this type of import.
Because * you don't know what name you import, it's likely to overwrite the name you've defined before. And the readability is extremely poor, there is no problem when importing in an interactive environment.
* If the name in module.py is pre-added _, i.e. _money, then the From module import *, then _money cannot be imported

also supports importing multiple lines
From My_module import (Read1,                    read2, Money                    )

Note: The win system does not use relative paths.

Loading and modification of modules

For performance reasons, each module is imported only once, put into the dictionary sys.modules, if you change the contents of the module, you must restart the program, Python does not support reloading or uninstalling the previously imported modules,

Some students may think of removing a module directly from Sys.modules can not be uninstalled, note that you deleted the Sys.modules module object may still be referenced by other program components, and therefore will not be cleared.

In particular, we refer to a class in this module, which produces many objects with this class, so that these objects have references to this module.

If it's just a module that you want to test interactively, use importlib.reload (), e.g. import importlib; Importlib.reload (ModuleName), which can only be used for test environments.

def func1 ():     Print ('func1')
aa.py
Import Time,importlib Import AA Time.sleep (a) # importlib.reload (aa)aa.func1 ()
Test Code

In 20 seconds of waiting time, modify the contents of func1 in aa.py, waiting for the result of test.py.

Open importlib comments, re-test

Execute the module as a script

Python's modules and packages

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.