Python Basics-Modules

Source: Internet
Author: User

1, Module Introduction

Many functions are grouped by category and placed in separate files. In Python, a. py file is called a module. In other languages, it is called a class library.

What are the benefits of using modules?

(1) Greatly improve the maintainability of the Code;

(2) Writing code does not have to start from scratch.

Categories of modules:

Built-in modules, third-party modules, automatic modules.

2. Import Module

Modules should be imported first, then used.

(1) Import module method

Import Module  from import xx             # points represent the folder hierarchy. You can import only one of these functions.   from import xx as rename   # Specify an alias for the imported module from Import *              # Import all the functions in the module

(2) Search path

the Import module actually tells the Python interpreter to explain the py file. When the module is imported, it is based on the sys.path path from top to bottom, and once it is found, it is imported and stopped for searching.

ImportSYSPrint(Sys.path) The result is: ['c:\\users\\sea\\pycharmprojects\\untitled', 'c:\\users\\sea\\pycharmprojects\\untitled', 'C:\\users\\sea\\appdata\\local\\programs\\python\\python36-32\\python36.zip', 'C:\\users\\sea\\appdata\\local\\programs\\python\\python36-32\\dlls', 'C:\\users\\sea\\appdata\\local\\programs\\python\\python36-32\\lib', 'c:\\users\\sea\\appdata\\local\\programs\\python\\python36-32', 'c:\\users\\sea\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages'] #这是第三方模块

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

such as: sys.path.append ("e:\\") will also add the E-disk to the directory

3, third-party module installation

(1) Installation with PIP3

The PIP3 is already installed in the Python3. You can see it under the Python installation directory Scripts folder.

The PIP tool in Python2 needs to be installed first.

Example: Installing the Requests module PIP3 install requests

(2) Source code installation

① first download the source code (tar file), unzip to find the setup.py file

②CD switch to this extract directory, execute command: Python setup.py install

Python Basics-Modules

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.