码迷,mamicode.com
首页 > 编程语言 > 详细

Python 导入模块的两种方法:import xxx 和from...import xxx

时间:2019-12-07 21:30:05      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:hellip   启动文件   copy   help   code   win   启动   too   文件   

 

import 方式导入模块

import tool.getsum.add
# 导入模块,优先会从启动文件的当前目录开始寻找
# 如果找到,就使用
# 如果找不到,会在系统模块存放目录去

tool.getsum.add.add2num(2,5)

 

      示例:

C:\Users\Tom> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print("Current Time: %F" % time.time())
Current Time: 1575719563.011607
>>> print("Current Time: " + time.strftime("%Y-%m-%d %X"))
Current Time: 2019-12-07 19:57:07

 

from…import

  • 导入模块

from tool import add
add.add2num(3,9)

# import tool.add
# tool.add.add2num(3, 9)

 

  • 导入模块中的标识符

    • 情况一,指定标识符来导入
# from ... import ....
# from 什么什么模块 import 什么什么标识符


from getsum.add import add2num

add2num(3,7)

import 的标识符,指定了什么,才能用什么。 没有指定标识符不可用
    • 情况二,导入模块中的全部标识符,通过*代表一切
from getsum.add import *

add2num(3,7)
print(name)
print(age)

这种导入方式,要注意名称的冲突

 

 

Python 导入模块的两种方法:import xxx 和from...import xxx

标签:hellip   启动文件   copy   help   code   win   启动   too   文件   

原文地址:https://www.cnblogs.com/morgan363/p/12003118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!