Python module (1), python module (

Source: Internet
Author: User

Python module (1), python module (

During the development of computer programs, as the program code is written more and more, the code in a file will become longer and longer, making it increasingly difficult to maintain.

In order to write maintainable code, we divide many function groups into different files, so that each file contains less code, many programming languages use this method to organize code. In Python, A. py file is called a Module ). For example, OS is a system-related module, and file is a file-related module.

The biggest benefit of the module is that it greatly improves the maintainability of the Code. Second, you do not have to write the code from scratch. When a module is compiled, it can be referenced elsewhere.

There are three modules:

  • Custom Module
  • Third-party module
  • Built-in modules
Import Module

The module import method is as follows:

1 import module2 from module import xx3 from module. xx import yy as [rename] 4 from module import * 5 _ import _ ('module') # import the module as a string

View the path of The py Module

1 import sys 2 print (sys. path) 3 4 # result 5 ['e: \ python \ coding \ day8', # path of the current python file 6'e: \ python \ pyInstall \ py3 \ python36.zip ', 7'e: \ python \ pyInstall \ py3 \ DLLs', 8'e: \ python \ pyInstall \ py3 \ lib ', 9 'e: \ python \ pyInstall \ py3', 10'e: \ python \ pyInstall \ py3 \ lib \ site-packages ']
Custom Module

If the python module path does not contain the package to be imported, you can use sys. path. append ('path') to add the module path.

1 import sys2 import os3 dirr = os.path.dirname(os.path.abspath("../day6/fs1.py"))4 sys.path.append(dirr)
Third-party module

Python has many third-party modules, which provide better performance than built-in modules.

pip3 install [module_name]

You can use the preceding command to install it in the operating system command line.

 

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.