Python中如何调用不同包中的方法?

1 个回答|55 次阅读
Coco老师 - 科普专家
擅长:编程教育

直接使用“import”命令导入包,然后用“包名.函数名”的方式就可以调用不同包中的方法了

示例如下:

调用time包跟requests包,计算获取网页信息所需要的的时间

import time
import requests
time1 = time.time()
html = requests.get(url='https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&
wd=python%E6%B1%89%E5%8C%96%E6%80%8E%E4%B9%88%E7%94%A8&oq=mysql%2520%25E5%25AD%2597%25E6%25AE%25B5%25E4%25B8%258D%25E4
%25B8%25BA%25E7%25A9%25BA&rsv_pq=8309d0dd0008d294&rsv_t=4e84X%2FfFdIhy16hUsU0rH0tyDn8hNcYx1%2B
%2BDAVTRScHP9Inci359AAWXyrk&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&inputT=875&rsv_n=2&rsv_sug3=182&rsv_sug4=875')
time2 = time.time()
print(time2-time1)