Python3 entry learning four.md

[ TOC]

15. Python module###

Description: A module is a more advanced package, and a module is a program; actually it is to import functions written in python files into other py files for calling;

The previous knowledge points:

Related knowledge points:

Module advantages:

(1) Import and select execution

Case:

# /usr/bin/python3
# Import external module verification

## Project structure
# Day4
# │  demo4.1.py
# │  TemperatureConversion.py

########### Case 1##############
# Method 1: import TemperatureConversion
# Method 2: from TemperatureConversion import c2f,f2c #Import the specified module(No need to consider namespace)
# Method 3: from TemperatureConversion import*    #This method is not recommended(May cause confusion in the namespace)
# Method: import TemperatureConversion.c2f  #Import the specified module of the specified package
# Method 4: as follows (often)
import TemperatureConversion as Temp

print("32 degrees Celsius= %.2f degrees Fahrenheit"%Temp.c2f(32))print("99 degrees Fahrenheit= %.2f degrees Celsius"%Temp.f2c(99))

########### Case 2##############
print(Temp.__name__) #The module name is displayed

############# Results of the###############
# 32 Celsius=89.60 degrees Fahrenheit
# 99 Fahrenheit=37.22 degrees Celsius
# TemperatureConversion

# """""""""""""" TemperatureConversion.py """"""""""""""" # 
def c2f(cel):'''Celsius to Fahrenheit'''
 fah = cel *1.8+32return fah

def f2c(fah):'''Fahrenheit to Celsius'''   
 cel =(fah -32)/1.8return cel

def test():print("Test 0 C= 0.2f F",c2f(0))print("Test 0 F= 0.2f C",c2f(0))

# Let Python know whether to run or import to the module
if __name__ =='__main__':  #Only execute test in its own module file()print("__name__The module name is displayed in the main program,If executed in the definition declaration file, display main: ",__name__)   #In this program TemperatureConversion.Shown in py is main(The main program call is still the module name)test()

(2) Search path and package
The above cases are that the module and the main executable file are in the same directory, but in actual development, they are often in other file subdirectories;

>>> import sys
>>> sys.path
['',' D:\\Python3\\python37.zip','D:\\Python3\\DLLs','D:\\Python3\\lib','D:\\Python3','C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages','D:\\Python3\\lib\\site-packages'(recommend),'D:\\Python3\\lib\\site-packages\\easygui-0.98.1-py3.7.egg']  #When importing the module,Python queries and imports module information in the above path

# Can use sys.path.append()Join the path to it

Steps to create a package:

Case:

#! /usr/bin/python3
# Module: case package import module main program

## Project structure
# │ demo4.2.py
# ├─Demo
# │  │  Module.py
# │  │  __init__.py

from Demo.Module import success  #Find the module under the specified Demo package and import the functions in the module(Can be called directly)success()  #Direct call

########## Results of the##############
# Dear,You successfully imported the functions in the module from the package to the main program,Yes!

#""""""""""""""" Demo\Module.py """"""""""""""#
#! /usr/bin/python
# Features:Module test program
def success():print("Dear,You successfully imported the functions in the module from the package to the main program,Yes!")

(2) Supplementary note
Description: The Python standard library comes with some commonly used modules, but some high-level usage still needs to check the official website Document;

Python IDLE - Help - Python Doc(You can press F1)
https://docs.python.org/3.7/

# Publish your own module pypi module
Distributing Python Modules :publishing modules for installation by others

# Extension and embedding c/c++
Extending and Embedding :tutorial for C/C++ programmers

# API interface use
Python/C API : reference for C/C++ programmers

# supplement:
PEP is the abbreviation of Python Enhancement Proposals(The meaning of the Python enhancement proposal,The technical specifications used to standardize and define various enhancements and derivative functions of Python are the basis for developers to follow together),

Important ways to learn a module:

>>> import time
>>> dir(time)>>>help(time)
# Note that when importing a module is*number,In existence__all__In this case, only the following modules will be imported
>>> timeit.__all__
[' Timer','timeit','repeat','default_timer']>>> timeit.__file__   #Module source code file
' D:\\Python3\\lib\\timeit.py'>>> timeit.__doc__ #Display help can be formatted output with print

Recommended Posts

Python3 entry learning four.md
Python3 entry learning three.md
Python3 entry learning one.md
Python3 entry learning two.md
Python entry-3
python learning route
python list learning
03. Operators in Python entry
Python function basic learning
python_ crawler basic learning
python_ regular expression learning
Python entry tutorial notes (3) array
Python regular expression quick learning
Python programming Pycharm fast learning
Getting started python learning steps
Python magic function eval () learning
05. Python entry value loop statement
Python regular expression learning small example
Learning Python third day one-line function
Python entry notes [basic grammar (below)]
Python from entry to proficiency (2): Introduction to Python
Learning path of python crawler development
Python learning os module and usage
Two days of learning the basics of Python