python3精要(35)-模块(1)-import
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相关推荐
- python3精要(55)-模块,私有方法
欧几里得度量(euclidean metric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的自然长度(即该点到原点的距离).在二维和三维空间中的欧氏距离就是 ...
- python3精要(35)-wxPython(1)-简介与开源协议
wxPython是Python编程语言的跨平台GUI工具包.它允许Python程序员简单易用地创建具有健壮.功能强大的图形用户界面的程序.它是作为一组Python扩展模块实现的,这些模块包装了流行的w ...
- linux环境下安装python3以及安装cx_Oracle模块需要注意的事项
linux环境下安装python3以及安装cx_Oracle模块需要注意的事项 python3的安装 1. 首先安装python3之前首先要安装python所依赖的环境 yum install gcc ...
- python oserror路径not found_详谈Python3 操作系统与路径 模块(os / os.path / pathlib)
python如何判断一个目录下是否存在某个文件?如果小编突然在人群中很疯或者沉默,那时一定很难过. 1.使用os模块 用os模块中os.path.exists()方法检测是否存在test_file.t ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- Python3.7中time模块的time()、perf_counter()和process_time()的区别
Python3.7中time模块的time方法.perf_counter方法和process_time方法的区别 1. time.time()方法 2. time.perf_counter()方法 3 ...
- python3的数据类型以及模块的含义
python3的数据类型以及模块的含义 购物车 转载于:https://blog.51cto.com/11834445/1884901
- python3.7导入gevent模块报错的解决方案
python3.7导入gevent模块报错的解决方案 参考文章: (1)python3.7导入gevent模块报错的解决方案 (2)http://www.cnblogs.com/eva-j/p/939 ...
- 电脑安装python3.7说缺少-centos7:python3.7 缺少_ssl模块问题
在centos7上安装python3.7,很多时候提示缺少安装_ssl模块,这导致很多依赖于ssl的模块无法正常安装,如ulib3,requests. 百度网上也提供很多方法,诸如安装liber.ss ...
最新文章
- 《精通自动化测试框架设计》—第1章 1.3节五天太久,还能压缩吗
- 让Sandcastle为你的类库生成类似于MSDN的帮助文档
- Linux内核源代码分析-第三章 内核体系结构概述-1
- 单片微型计算机与一般微型计算机相比,单片机习题与思考题.doc
- NOIP2018退役记
- python绘制指数函数图像及性质_指数函数图像及其性质正式版
- boost::signals2::slot相关的测试程序
- centos php imap,centos 5.5 64 php 添加imap 模块记录
- TensorFlow发布全新版本,又会带来哪些变革?
- ACM算法 -- 数论 -- 开灯关灯问题(数论,整数分解,因子个数,公式推导)
- Python自动对Word文件中Python程序进行着色
- linux 内核 ide,Linux设备驱动程序学习(7)-内核的数据类型
- MATLAB机械动力分析,基于MATLAB的柔性机械臂动力学分析.pdf
- 如何把 .mobi 格式的电子书转换成 PDF
- 个人计算机键盘上的按键击键声音小,电脑键盘按键会双击怎么办
- php 外包 上海,== | php外包与php技术服务商
- 上班/读研办公室摸鱼如何快速切屏不留痕迹?Win10切屏快捷键赶紧收藏起来
- 基于微信小程序云开(统计学生信息并导出excel)2.0版
- android 蓝牙BluetoothAdapter的介绍
- 华清远见重庆中心-JS技术总结