Python Note 4# module modules

Source: Internet
Author: User

▲ Module Modules

In order to write maintainable code, we can put the code in separate files. In Python, a. py file is called a module.

When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.

The use of modules avoids collisions between function names and variable names, but also be aware that you should try not to conflict with the built-in functions (built-in function) names.

To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

There will be a __init__.py file under each package directory. This file must exist, otherwise Python will treat the directory as a normal directory instead of a package. __init__.py can be an empty file, or it can be used by Python code. The __init__.py itself is a module whose module name is the package name.

The package structure can also be organized in multiple tiers. For example

There is a subpackage package in the MyPackage package, and there is a abc.py file in the subpackage. Then the abc.py file module name is as follows:

MyPackage.subPackage.abc

The sample code is as follows:

#CODING:GBK__author__='Wddoer'ImportSYSdefTest ():#sys.argv Store command line all parameters with Listargs =SYS.ARGVifLen (args) = = 1:        Print('Hello, world!.')    elifLen (args) = = 2:        Print('Hello,%s!.'% args[1])    Else:        Print('Too many arguments!')#when the command line runs, the IF statement is true and the other places are false. Used for testing.if __name__=='__main__': Test ()

Scope:

There are functions and variables that we want to use only inside the module. In Python, this is done by the prefix _.

Functions and variables such as _xxx and __xxx are non-public and should not be directly referenced from a programming habit.

▲ Installation of the module

Installing a third-party module is done through the Setuptools tool. Python has two package management tools that encapsulate Setuptools: Easy_install and Pip. Currently the official recommended PIP.

Common third-party libraries: PIL (Python Image Library), Mysql-python, NumPy, etc.

▲ Search Path of module

When attempting to load an imported module, Python searches for the corresponding. py file under the specified path.

By default, the Python interpreter searches the current directory, all installed built-in modules, and third-party modules. The search path is stored in the PATH variable of the SYS module.

Import sys>>> sys.path['c:\\windows\\system32\\python34.zip ' ' C:\\python34\\dlls ' ' C:\\python34\\lib ' ' C:\\python34 ' ' c:\\python34\\lib\\site-packages ']

There are two ways to add your own search path.

First, add directly using the Sys.path.append () method. This method is modified at run time and fails after the run is finished.

Second, set the environment variable Pythonpath, and the contents of the environment variable will be automatically added to the module search path.

▲__future__

Python provides the __future__ module to import the next new version of the feature into the current version. So we can test and test some features of the new version again in the current version.

Python Note 4# module modules

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.