What are the modules in Python? 3 minutes to understand the module problem in Python

Source: Internet
Author: User
modules in Pythonis a python file that ends with a. Py, contains Python object definitions and Python statements, and allows you to logically organize your Python code snippets. Assigning the relevant code to a module will make your code better and easier to understand. modules in PythonCan define functions, classes and variables, and modules can also contain executable code.

First, let's look at the differences between the modules , libraries, and packages in Python.

the modules in Python are:
Self-contained and organized code fragments for modules.
The representation is: the written code is saved as a file. This file is a module. sample.py where the file name Smaple is the module name.

Python packages are:
A package is a hierarchical file directory structure that defines a Python application execution environment consisting of n modules or N sub-packages.
Popular point: The package is a directory containing __init__.py files, which must have this __init__.py file and other modules or sub-packages.

The Python library is:
The reference to other programming languages refers to the combination of code in Python that completes a certain set of functions for the user to use. In Python is the form of packages and modules.

Example

The following example is a simple module support.py:

support.py module:

def print_func (PAR):       print "Hello:", par   return

Since the module in Python is a file, how do we search for it in some moments?

Search Path

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 not found, Python will look at the default path. Under UNIX, the default path is generally/usr/local/lib/python/.

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.

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.