Python's modules and packages

Source: Internet
Author: User
Tags python script

Introduction to a Python module

Python module:
A code-heavy program can be segmented into several pieces of code that are organized, independent but interacting with each other, and these self-contained, organized snippets of code are modules

The module behaves as a code file that ends in. py in physical form
A file is seen as a standalone module, and a module can be viewed as a file
The file name of the module is the name of the module plus the extension. py
Each module has its own namespace
Python allows "import" of other modules for code reuse, which also enables the organization of separate code files into larger program systems
In Python, a module is also an object
All variables in the top-level definition (global variable) of a module are imported as properties of the module being imported

Two Python program schemas:

A python program typically includes a top-level file and other module files (0, 1, or more)
Top-level files: Contains the main control flow of the program
Module files: Provides a variety of functional components for top-level files or other modules
When the module is first imported (or overloaded), Python immediately executes the top-level program code of the module file (code that is not inside the function), and the code in the body of the function is not executed until the function is called.
Python comes with a module called Python's standard library module
Module Search:
The Python interpreter must first find the corresponding module file in the Import module
The program's home directory
PYTHONPATH (if this variable is set)
Standard link Library Directory
The contents of any. pth file (if a. pth file exists)
The four components are combined and the path contained in Sys.path, while Python chooses the file that imports the filename in the first symbol in the search path

Module import requires a process called "path search", that is, in the file system "pre-defined area" to find the MyModule file, these predefined areas are just a collection of your Python search path, path search and search path is a two different concepts , The former refers to the operation of finding a file, which is to query a set of directories.
The default search path is specified at compile or install time and can be modified in one or two places
One is the PYTHONPATH environment variable that launches the shell or command line of Python. The content of the variable is a set of directory paths separated by colons. If you want the interpreter to use this variable, be sure to set or modify the variable before starting the interpreter or executing a Python script.
After the interpreter is started, the search path can also be accessed, which is stored in the Sys.path variable of the SYS module. However, it is not a colon-delimited string, but it contains a list of each independent path. The following is a sample of a Unix machine search path. Remember that the search path is generally different under different systems.

This is a list, so we can make modifications, if you need to import the module's directory is not in Sys.path, you can use append () to do this list operation to import this module

Add the above directories to the Sys.path

View

Module Import

As long as this list contains this file, it will be imported correctly, of course, this method is to import it to the end of the file, you can also use Insert () to import it to the specified location

Import of modules in three Pycharm 1 configuration in the same folder

A defines a module that is imported

B Import module to target module

C uses name to get the name of the module, when it is inside the module, its value is mian, and if it is imported, it is named module name
A module file can support both top-level execution (as a top-level file) or be imported (as a module file)
Each module has a built-in property named name, and Python automatically sets the property
If the file is executed as a top-level program file, the value ofname is "main" at startup
If it is imported, the value of name is the module name
You can detect your name order in the module file, ING, to run the specified code at execution time, often for self-testing of modules


D import it into the execution module to view:

E uses the IF statement to determine that the execution results of the imported module are not executed in the Import module:

F Import and verify it:

G Import the configuration into the specified template: (ctrl+alt+s) to open its configuration:

2 Place the imported module and the import module in a different folder

Package: A package is used to merge a set of modules into a directory, as well as a package, directory name, and package name
A package is a hierarchical file directory structure that defines a Python application execution environment consisting of modules and sub-packages
Based on the package, Python can specify the module's import path when executing the module import
Import dir1.dir2.mod1
To use the following package1, the Py_pkg_mod container must search the path in the module
Import package1.mod1
The init. py file must be in each directory within the path of the package import statement
init. Py can contain Python code, but is usually empty, used only to act as a hook for package initialization, to generate a module namespace for the catalog, and to implement the from* behavior when using catalog Import
Creation of packages in Pycharm:
Use the Alt+ins shortcut key to specify whether to create a file or a package that is a Python package with the init. py.


A modify its configuration so that files of different folders can be imported directly (select sources Root)

b You can import and run it again:

C Cancel will not run:

D Create the init. py file in the same folder as the imported module and import its local module into this init. py file

E Import the module and run it in the destination file, it will work

Three from ... import ... The application

For-import statements
Often used to import the specified module into the current namespace (no need to add the module name)
From module import Name1[,name2[namen]]
Both Import and from are invisible assignment statements
Import assigns the entire module object to a variable name (module name)
From assigns one or more variable names to an object of the same name in the module in which the module is imported, if the current environment has the same name as the import module, it will be overwritten with the newly imported name
The module is the name space
Working mechanism of Import
1 Module Files found
Search for module files under the specified path
2 Compiling into byte code
The file is compiled when it is imported, so the. PYc bytecode file for the top-level file is discarded after internal use, and only the files that are imported are left. pyc file
3 executing the code of the module to create the object it defines
All the statements in the module file are executed sequentially, from the beginning to the beginning, and any copy of the variable name in this step will produce the properties of the resulting module file
Note: The module does not perform the above steps until the first import.
Subsequent import operations are simply extracting the loaded module objects in memory
Reload () can be used to reload the module
A module import for the same folder:

B definition of global variables

C to import the global variables and run them, you can run them.

D to import the function and run it, you can run it.

E Modify the module name or function of the import module, the adorner name can be modified using as:

Iv. installation of third-party modules

Access to basic information related software: (itchat)
1 Download and install Python-pip

This means that the installation was successful:

2 Installing the Itchat (plug-in)
Install in a networked environment
Install with PIP

Installing with Pycharm
Use Ctrl+alt+s to enter this interface and press the plus sign (+) in the upper right corner

Go to find itchat and press the Intall package in the lower left corner to install

Python's 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.