程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to reference other modules in Python

編輯:Python

Catalog

One 、 Preface

Two 、 Import and use standard modules

3、 ... and 、 Third party module download and installation

One 、 Preface

stay 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 modules

stay 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 installation

It'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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved