Python Common Module Learning

Source: Internet
Author: User

1. Module Introduction

2.time & DateTime Module

3.random

4.os

5.sys

6.shutil

7.json&pickle

8.shelve

9.xml processing

10.yaml processing

11.configparser

12.hashlib

13.subprocess

14.logging Module

15.re Regular Expressions

1. Module

A module is a collection of code that implements a function with a single piece of code.

Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions are required to complete (the function can be in a different. py file), and the Code collection consisting of N. py files is called a module.

such as: OS is a system-related module; file is a module related to the operation of files

The modules are divided into 3 types:

    • Custom Modules
    • Built-in Modules
    • Open source Module
Custom modules:

1. Defining the Module

Scenario One:

Scenario Two:

Scenario Three:

????????????????????????????????????

2. Import the module

There are several ways to import the module:

1 Import Module 2  from Import xx 3  from Import xx as rename 4  from Import *

The import module actually tells the Python interpreter to explain the. py file

Import a py file, the interpreter interprets the py file

Import a package, the interpreter interprets the _init_.py file under the package

So the question is, which path will be used as the benchmark when importing the module? namely: Sys.path

ImportSYSPrintsys.path results: ['/users/wupeiqi/pycharmprojects/calculator/p1/pp1','/usr/local/lib/python2.7/site-packages/setuptools-15.2-py2.7.egg','/usr/local/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg','/usr/local/lib/python2.7/site-packages/mysql_python-1.2.4b4-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/xlutils-1.7.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/xlwt-1.0.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/xlrd-0.9.3-py2.7.egg','/usr/local/lib/python2.7/site-packages/tornado-4.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/backports.ssl_match_hostname-3.4.0.2-py2.7.egg','/usr/local/lib/python2.7/site-packages/certifi-2015.4.28-py2.7.egg','/usr/local/lib/python2.7/site-packages/pyopenssl-0.15.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/six-1.9.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/cryptography-0.9.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/cffi-1.1.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/ipaddress-1.0.7-py2.7.egg','/usr/local/lib/python2.7/site-packages/enum34-1.0.4-py2.7.egg','/usr/local/lib/python2.7/site-packages/pyasn1-0.1.7-py2.7.egg','/usr/local/lib/python2.7/site-packages/idna-2.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/pycparser-2.13-py2.7.egg','/usr/local/lib/python2.7/site-packages/django-1.7.8-py2.7.egg','/usr/local/lib/python2.7/site-packages/paramiko-1.10.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/gevent-1.0.2-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/greenlet-0.4.7-py2.7-macosx-10.10-x86_64.egg','/users/wupeiqi/pycharmprojects/calculator','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python27.zip','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-darwin','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac/ Lib-scriptpackages','/USR/LOCAL/CELLAR/PYTHON/2.7.9/FRAMEWORKS/PYTHON.FRAMEWORK/VERSIONS/2.7/LIB/PYTHON2.7/LIB-TK','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/lib-old','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/site-packages','/library/python/2.7/site-packages']
View Code

If the Sys.path path list does not have the path you want, you can add it by sys.path.append (' path ')

Access to various catalogs via OS module

1 Import SYS 2 Import OS 3 4 Pre_path = Os.path.abspath ('.. /')5 sys.path.append (Pre_path)
View Code

2. Open source module

First, download the installation

There are two ways to download the installation:

yumpipapt-get
Way OneMode two

Note:

Python Common Module Learning

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.