Python module knowledge 1: module knowledge introduction, python knowledge Introduction

Source: Internet
Author: User

Python module knowledge 1: module knowledge introduction, python knowledge Introduction

Modules are code classificationCan define functions, classes, and variables, and allocate relevant code to a module, which makes your code easier to use and easier to understand and simplify. The module is called a class library in java.

How the module exists:

The module can be a single. py file or a single file (which stores n. py files ).


1. Module classification:

  • Built-in modules:For example, OS and sys are two very common modules that interact with the operating system. file is a module related to file operations. Commonly used modules include logging, time/datetime, json/pickle.

  • Custom module:Your own py file or folder (which can contain multiple py files)

  • Third-party module:Such as requests and math


2. Use of modules: the principle is to be used after pilot Import

It is best to put the module and the execution file in the same directory. The import method can use import or from.

The main syntax format is as follows:

  • Import to a single module: for example, to import a single. py file or s1.py file, you can directly use import + file name

Import s1

S1.login () # Use the login function in s1

  • Import files in the folder: for example, if s1.py is in the lib folder, you can use the import folder name. file name.

Import lib. s1

Lib. s1.login () # usage

  • Import a single module from: for example, import a single. py file, s1.py,

From s1 import login

Login ()

  • From imports all functions at a time: But this method is not recommended.

From s1 import *

Login ()

Logout ()


  • If it is a folder, you can set an alias after import, and then you can use the alias to call it directly.

From lib import s1 as lib_s1

From scr import s1 as scr_s1


3. Module priority rules:

Python first searches for the directory where the project is located. If the directory does not exist, it searches for other default directories in python. The priority is from top to bottom. For specific priorities, see sys. path.

If the file is not in the system search directory, you can use append to add it.

Case 1: The module finds the directory priority.

import sys
for item in sys.path:
print(item)

Search priority:

Case 2: If s2 is not in the system directory, you can add it first and then import the s2 file:

Import sys
Sys. path. append ('d: \ ') # import the D folder
Import s2 # file name

4. Third-party module Import

  • Install and download pip3:

1) pip3 install module. Python3 comes with pip3. You need to add the directory of pip3 to the environment variable, and then you can directly import the module.

2) pip3 uninstall + uninstall the module


Import module requests

Case:

Step 1: Enter the Directory

Start-cmd. Open the command edit box and enter the following command (cd + space + pip3 directory) To Go To The pip3 directory:

Input command:CdC: \ Users \... \ AppData \ Local \ Programs \ Python \ Python36 \ Scripts

Step 2: Install wheel

Input: pip3 install requests


  • Download the source code, install it, download the source code, and then execute


5. Special variables in the module:

1) view all built-in Variables

Execution result:


2) Comment Information _ doc __

Import s2

Print (_ doc _) # obtain the annotation information and obtain the content in the py program document ""


3) obtain the path _ file _ of the current program __

# Print (_ file _) # obtain the path of the current program

Import OS

Print (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects \ study2017 \ s2.py

Print (OS. path. dirname (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects \ study2017

Print (OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects


4) print (_ package _) # obtain the package of the programThe file import contains the package. If your package is imported, None is displayed.


5)Main File Execution _ name __= = "_ main __"

# It is executed only when the current file is executed. The special Variable _ name __= = "_ main _" in the current file. If it is imported, it will not be executed. Therefore, you can write it in the main file, which can be restricted, it is executed only in the main program and not when it is imported as a module


Def run (): # define the run Function

Print ("run ")

Run ()


If _ name __= = "_ main _": # Run only in the main program

Run ()



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.