Python basic syntax - modules, built-in modules, third-party modules, third-party module installation

# A module is the highest-level organizational unit in Python. It packages program code and data for reuse, and contains multiple functions and attributes to implement a certain type of business. # A module is a toolkit to implement a certain type of business 
. To use the tools in the toolkit (usually functions), you need to import this module. Modules are also called packages or libraries. 
#How to import modules? 
# import module name is essentially introducing another python file into a python file 
# The introduced module can also set an alias in the document: import module name as alias 
# Introduce part of a complex module from module name import submodule # 1. Built-in module random 
import random 
r=random.randint(1,6)#randint means randomly generating an integer between 1 and 6 
print(r) 
r1=random.uniform(1,6)#means 1 to 6 Randomly generate a decimal 
print(r1) 
r3=random.choice([1,132,32,43,43,454,46,56,55,76,7]) #Randomly obtain a value in a sequence 
r4=random.random () #Generate 0-1 random floating point number #2. Built-in module time 
import time 
#Countdown 
i=10 
while i>=0: 
   print("Countdown:",i) 
   time.sleep(1) 
   i-=1



#3. Built-in module datetime 
import datetime 

#Get the current date and time 
now=datetime.datetime.now() 
print(now) 

#Get a specified time 
d1=datetime.datetime(2018,2,13,5,23,45) 
print(d1) 

#Convert date to string 
s1=d1.strftime("%Y year-%m month-%d day %H:%M:%S") 
print(s1) 

#Convert string to date (the format must be Consistent) 
s2="2018/02/13 05:23:45" 
d2=datetime.datetime.strptime(s2,"%Y/%m/%d %H:%M:%S") 
print(d2,type (d2)) 
#Return as follows: 
2022-06-28 11:22:48.655992 
2018-02-13 05:23:45 2018-02-13 
05:23:45 
2018-02-13 05:23: 45 <class 'datetime.datetime'> 4. Built-in module urllib 
from urllib import request 
url="http://www.baidu.com"



data=request.urlopen(url).read() #Send the request and read the data
print(data.decode()) #decode() decoding: convert binary into characters 

with open(r"0413.html","wb") as f: 
   f.write(data)#Write the content in the Baidu link In 0413, 


#python modules are placed in the lib folder: C:\Users\username\AppData\Local\Programs\Python\Python310\Lib #Introduce the 

python file you wrote 
import supermarket system #If you introduce it directly like this , the default programs in the module will be executed.

When introducing a module, the default code is also executed. At this time, the imported module must be transformed and the default executed statement is placed in the main function. def main():
Default
               executed function module
if __name__== '__main__':
    main()
#This statement means: the main function will only be executed when the python file of the module itself is executed. The main function will not be executed when the module is referenced by the outside world. Modules are divided into 
: built-in modules: such as random, and Third-party modules: need to be downloaded before they can be introduced

Python third-party package installation method

Method 1 (using pip, you need to upgrade before using it for the first time):

Open cmd directly and enter the command.

pip tool upgrade: python -m pip install --upgrade pip

Install module: pip install module name

Uninstall the module: pip uninstall module name

View installed third-party packages: pip list

Display module information: pip show module name

pip installation can specify the version: pip install django==3.1.5

Method 2 (Tsinghua Mirror):

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple module name

Method 3 (online download):

Step1: Find the required package https://pypi.python.org online and download it.

Step2:

Whl installation method: cmd enter the directory where the whl file is located, execute pip install file name

Folder installation method (setup): cmd enter the directory of the installed package, find the setup.py file, and then enter python setup.py install      

(You can also use this method to install pip when you don’t have the pip tool)

The 4th method (download from pycharm):

【File】→【Settings】→【Project】→【Project Interpreter】

Guess you like

Origin blog.csdn.net/qq_40333984/article/details/125468654