from和import 导入模块
import 读取整个模块,而from只导入模块中的某些变量和函数

# -*- coding: utf-8 -*-
"""
Spyder EditorThis is a temporary script file.
"""
import testmodule
print(testmodule.x)
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:54:16 2021@author: Administrator
"""
x=11
y=22
def testfun2():print("testfun2")
def testfun1():print("testfun1")
runfile('G:/learn/2.py', wdir='G:/learn')
11

import导入时,被导入模块的全部顶层变量和语句将被执行

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:54:16 2021@author: Administrator
"""
x=11
y=22
def testfun2():print("testfun2")
def testfun1():print("testfun1")print("testmodule importing...")#if __name__ == "__main__":
#    print("testmodule runing...")
#    testfun1()
#    testfun2()
# -*- coding: utf-8 -*-
"""
Spyder EditorThis is a temporary script file.
"""
import testmodule
print(testmodule.x)
testmodule.testfun1()
print(testmodule.__name__)
print(__name__)
runfile('G:/learn/2.py', wdir='G:/learn')
testmodule importing...
11
testfun1
testmodule
__main__

被导入 模块也可以直接执行

runfile('G:/learn/testmodule.py', wdir='G:/learn')
testmodule importing...

通过Python中的if name == 'main’形式识别 是被导入 的还是直接执行的
下面是直接执行的

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:54:16 2021@author: Administrator
"""
x=11
y=22
def testfun2():print("testfun2")
def testfun1():print("testfun1")if __name__ == "__main__":print("testmodule runing...")testfun1()testfun2()
else:print("testmodule importing...")
runfile('G:/learn/testmodule.py', wdir='G:/learn')
testmodule runing...
testfun1
testfun2

下面是导入的

runfile('G:/learn/2.py', wdir='G:/learn')
testmodule importing...
11
testfun1
testmodule
__main__

被导入的__name__为模块名,当前执行的__name__为__main__
将被导入模块改为:

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:54:16 2021@author: Administrator
"""
x=11
y=22
def testfun2():print("testfun2")
def testfun1():print("testfun1")print(__name__)if __name__ == "__main__":print("testmodule runing...")testfun1()testfun2()
else:print("testmodule importing...")
runfile('G:/learn/testmodule.py', wdir='G:/learn')
Reloaded modules: testmodule
__main__
testmodule runing...
testfun1
testfun2
runfile('G:/learn/2.py', wdir='G:/learn')
testmodule
testmodule importing...
11
testfun1
testmodule
__main__

因此,被导入模块可改成以下形式,效果一样

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:54:16 2021@author: Administrator
"""
x=11
y=22
def testfun2():print("testfun2")
def testfun1():print("testfun1")if __name__ == "testmodule":print("testmodule importing...")
else:print("testmodule runing...")testfun1()testfun2()
runfile('G:/learn/testmodule.py', wdir='G:/learn')
testmodule runing...
testfun1
testfun2runfile('G:/learn/2.py', wdir='G:/learn')
testmodule importing...
11
testfun1
testmodule
__main__

python3精要(35)-模块(1)-import相关推荐

  1. python3精要(55)-模块,私有方法

    欧几里得度量(euclidean metric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的自然长度(即该点到原点的距离).在二维和三维空间中的欧氏距离就是 ...

  2. python3精要(35)-wxPython(1)-简介与开源协议

    wxPython是Python编程语言的跨平台GUI工具包.它允许Python程序员简单易用地创建具有健壮.功能强大的图形用户界面的程序.它是作为一组Python扩展模块实现的,这些模块包装了流行的w ...

  3. linux环境下安装python3以及安装cx_Oracle模块需要注意的事项

    linux环境下安装python3以及安装cx_Oracle模块需要注意的事项 python3的安装 1. 首先安装python3之前首先要安装python所依赖的环境 yum install gcc ...

  4. python oserror路径not found_详谈Python3 操作系统与路径 模块(os / os.path / pathlib)

    python如何判断一个目录下是否存在某个文件?如果小编突然在人群中很疯或者沉默,那时一定很难过. 1.使用os模块 用os模块中os.path.exists()方法检测是否存在test_file.t ...

  5. Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  6. Python3.7中time模块的time()、perf_counter()和process_time()的区别

    Python3.7中time模块的time方法.perf_counter方法和process_time方法的区别 1. time.time()方法 2. time.perf_counter()方法 3 ...

  7. python3的数据类型以及模块的含义

    python3的数据类型以及模块的含义 购物车 转载于:https://blog.51cto.com/11834445/1884901

  8. python3.7导入gevent模块报错的解决方案

    python3.7导入gevent模块报错的解决方案 参考文章: (1)python3.7导入gevent模块报错的解决方案 (2)http://www.cnblogs.com/eva-j/p/939 ...

  9. 电脑安装python3.7说缺少-centos7:python3.7 缺少_ssl模块问题

    在centos7上安装python3.7,很多时候提示缺少安装_ssl模块,这导致很多依赖于ssl的模块无法正常安装,如ulib3,requests. 百度网上也提供很多方法,诸如安装liber.ss ...

最新文章

  1. 主机配置_分享几套电脑主机配置
  2. Mac安装 ohmyzsh发生443错误
  3. 《乌合之众》读书笔记(part4)--含义最不确定的词语,往往拥有最强大的影响力
  4. matlab常用函数——数据类型函数
  5. 从真实项目中抠出来的设计模式——第三篇:责任链模式
  6. 你会用Java实现两个大数相加吗
  7. android语法高亮编辑器,HighlightTextEditor
  8. Android网络编程Socket【实例解析】
  9. 联合多企业成立泛娱乐IP联盟 迅雷将打造新内容消费生态
  10. day20/FileDemo1.java
  11. 通过 Nginx 来实现禁止国外IP访问网站
  12. R语言读取(加载)txt格式数据为dataframe、依据学号字段从dataframe随机抽取10位同学的数据
  13. 深入了解Excel工作表中的控件
  14. tableau中快速实现环比增长率计算
  15. 康托尔悖论:大全集不存在,即包含一切集合的集合是否存在
  16. memcached面试专题
  17. 局域网共享文件夹现在内存不足_局域网文件夹共享给指定用户的方法
  18. Out-of-Band(OOB)调研
  19. window系统 实现FFmpeg 录制音视频
  20. 解决移动端、苹果端、安卓端点击视频自动全屏问题的方法

热门文章

  1. Nginx+tomcat集群的session共享问题
  2. python List交集、并集、差集
  3. 不信任的 .exe 怎么办,用 Windows 沙盒啊!
  4. Gromacs文件-Chapter1
  5. 数据库事务的四大特性ACID
  6. jQ html5拖放
  7. 微信平台开发1--开发者模式基本配置
  8. PostgreSQL消息乱码的解决
  9. cellphonedb 及其可视化
  10. SpringBoot(一)——快速入门