One 、 Preface
Two 、 Import and use standard modules
3、 ... and 、 Third party module download and installation
One 、 Prefacestay Python in , In addition to customizable modules , You can also reference other modules , It mainly includes the use of standard libraries and third-party modules . Here are the introduction .
Two 、 Import and use standard modulesstay Python in , It comes with many practical modules , It's called the standard module ( It can also be called standard library ), For standard modules , We can use it directly import Statement into Python Use... In the document . for example , Import standard modules random( Used to generate random numbers ), You can use the following code :
import random # Import standard modules random
explain : When importing standard modules , You can also use as Keyword to give it an alias . Usually , If the module name is long , You can set an alias for it .
After importing the standard module , You can call the function provided by the module name . for example , Import random After module , You can call it randint() Function to generate a random integer in a specified range .
Generate a 0~10 Between ( Include 0 and 10) The code of random integer is as follows :
import random# Import standard modules randomprint(random.randint(0,10)) # Output 0~10 The random number
Execute the above code , It may output 0~10 Any number in .
except random Outside the module ,Python It also provides about 200 Multiple built-in standard modules , The clouds are covered Python Runtime services 、 Text pattern matching 、 Operating system interface 、 Mathematical operations 、 Objects are permanently preserved 、 The Internet and Internet Scripts and GUI Construction, etc .
In addition to the standard modules listed in the table above ,Python There are many other modules available in the , Readers can Python View in the help document for . The specific methods : open Python Installation directory Doc Catalog , The extension in this directory is .chm file ( Such as python370.chm) That is to say Python Help document for .
Open the file , Find the position as shown in the figure below and check it :
3、 ... and 、 Third party module download and installationIt's going on Python Program development , Except that it can be used Python Built in standard module outside , There are many third-party modules that can be used . For these third-party modules , Can be in Python Officially launched https://pypi.org/ Find .
When using third-party modules , You need to download and install the module first , Then you can import and use it as if you were using a standard library . This article mainly introduces how to download and install third-party modules . Download and install third-party modules can be used Python Provided pip Name the implementation ,
pip The syntax of the command is as follows :
pip <command> [modulename]
Parameter description :
command: Used to specify the command to execute . Common parameters are install( For installing third-party modules )、uninstall( Used to uninstall third-party modules )、list( Used to display installed third-party modules ) etc. .
modulename: Optional parameters , Used to specify the name of the module to be installed or uninstalled , When command by install or command by uninstall You can't omit .
for example : Install third party numpy modular ( For scientific calculation ), Complete the installation , The results shown in the figure below will be displayed .
This is about how to Python That's all for the articles that reference other modules in , More about Python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !