Python Fifth day Module (2)

Source: Internet
Author: User

module, which implements a set of code for a function with a mound code.

There are two ways to exist

1. Write to a folder

2.py file

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 may be required to complete (functions can be in different. py files), 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 three types:

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

1. Define the module

2. Import Module

Python is used more and more widely, and to a certain extent relies on it providing programmers with a large number of modules to use, and if you want to use modules, you need to import them. There are several ways to import a 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, is the module being imported based on that path as a benchmark? namely: Sys.path

ImportSYSPrintSys.path Result: The default path is a list. ['/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']

If the Sys.path path list does not have the path you want, it can be added by Sys.path.append (' path '). (for which file to execute, Python will automatically add the folder that contains the file to the path of the module)
Various directories can be obtained through the OS module, for example:

1 Import SYS 2 Import OS 3 4 Pre_path = Os.path.abspath ('.. /')5 sys.path.append (Pre_path)
__init__ is a special file, he will explain a folder by default, (a module must have a __init__ file, so that it is called a package, can be imported)Open source Module

Python Fifth day Module (2)

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.