首页 > 代码库 > python模块
python模块
模块
>>实现一类功能的一段程序,可以重用多个函数
>>导入模块
>>关键字import
#导入 import 模块名 #使用 模块名.方法名
>>> import math
>>> math.pi
3.141592653589793
>>sys模块:
>>在标准库中与系统功能有关的模块
>>> import sys >>> #查看版本信息 >>> sys.version ‘3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]‘
>>> import sys >>> #查看当前运行程序目录地址 >>> sys.executable ‘C:\\Python35\\pythonw.exe‘
>>> import sys >>> #返回windows操作系统版本信息 >>> sys.getwindowsversion() sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack=‘Service Pack 1‘)
>>> import sys >>> #返回现在导入模块的列表 >>> sys.modules.keys() dict_keys([‘keyword‘, ‘encodings.gbk‘, ‘platform‘, ‘bz2‘, ‘marshal‘, ‘html.entities‘, ‘idlelib.Bindings‘, ‘_weakref‘, ‘abc‘, ‘_warnings‘, ‘_signal‘,
‘idlelib.WindowList‘, ‘html‘, ‘ntpath‘, ‘_frozen_importlib‘, ‘traceback‘, ‘linecache‘, ‘urllib‘, ‘sys‘, ‘idlelib.RemoteDebugger‘, ‘tokenize‘,
‘_frozen_importlib_external‘, ‘locale‘, ‘idlelib.FileList‘, ‘_lzma‘, ‘tkinter.filedialog‘, ‘idlelib.AutoComplete‘, ‘idlelib.Debugger‘, ‘shlex‘,
‘_thread‘, ‘idlelib.RemoteObjectBrowser‘, ‘importlib._bootstrap‘, ‘warnings‘, ‘importlib.util‘, ‘_markupbase‘, ‘_socket‘, ‘_compression‘, ‘_io‘,
‘idlelib.AutoCompleteWindow‘, ‘idlelib.textView‘, ‘contextlib‘, ‘_collections_abc‘, ‘_struct‘, ‘argparse‘, ‘idlelib.Percolator‘, ‘encodings.aliases‘,
‘sre_parse‘, ‘importlib‘, ‘lzma‘, ‘tempfile‘, ‘_sre‘, ‘idlelib.CallTipWindow‘, ‘tkinter.font‘, ‘bdb‘, ‘idlelib.TreeWidget‘, ‘_stat‘, ‘textwrap‘,
‘idlelib.IdleHistory‘, ‘encodings.mbcs‘, ‘struct‘, ‘html.parser‘, ‘_imp‘, ‘idlelib.HyperParser‘, ‘tkinter.dialog‘, ‘stat‘, ‘random‘, ‘pydoc‘, ‘_locale‘,
‘idlelib.configDialog‘, ‘idlelib.ObjectBrowser‘, ‘weakref‘, ‘idlelib.ColorDelegator‘, ‘code‘, ‘importlib.machinery‘, ‘pkgutil‘, ‘builtins‘,
‘collections.abc‘, ‘idlelib.configHandler‘, ‘idlelib.help‘, ‘idlelib.ReplaceDialog‘, ‘os‘, ‘token‘, ‘idlelib.dynOptionMenuWidget‘, ‘idlelib.MultiStatusBar‘,
‘math‘, ‘_collections‘, ‘collections‘, ‘importlib._bootstrap_external‘, ‘idlelib.aboutDialog‘, ‘encodings‘, ‘idlelib.run‘, ‘idlelib.OutputWindow‘,
‘tarfile‘, ‘gettext‘, ‘enum‘, ‘idlelib.SearchDialogBase‘, ‘tkinter.messagebox‘, ‘tkinter.colorchooser‘, ‘string‘, ‘_codecs‘, ‘idlelib.PyShell‘, ‘io‘,
‘ast‘, ‘hashlib‘, ‘_heapq‘, ‘winreg‘, ‘__main__‘, ‘idlelib.IOBinding‘, ‘idlelib.Delegator‘, ‘threading‘, ‘idlelib.rpc‘, ‘select‘, ‘encodings.latin_1‘,
‘_tkinter‘, ‘idlelib.EditorWindow‘, ‘queue‘, ‘posixpath‘, ‘heapq‘, ‘idlelib.GrepDialog‘, ‘itertools‘, ‘os.path‘, ‘codeop‘, ‘_operator‘, ‘sysconfig‘,
‘copyreg‘, ‘_ast‘, ‘idlelib.ScrolledList‘, ‘_bootlocale‘, ‘_winapi‘, ‘inspect‘, ‘idlelib.PyParse‘, ‘idlelib.WidgetRedirector‘, ‘site‘,
‘idlelib.configSectionNameDialog‘, ‘types‘, ‘importlib.abc‘, ‘copy‘, ‘tkinter‘, ‘fnmatch‘, ‘subprocess‘, ‘idlelib.SearchDialog‘, ‘reprlib‘,
‘idlelib.SearchEngine‘, ‘selectors‘, ‘tkinter.commondialog‘, ‘_opcode‘, ‘_compat_pickle‘, ‘webbrowser‘, ‘tkinter.constants‘, ‘nt‘, ‘pickle‘,
‘_random‘, ‘idlelib.configHelpSourceEdit‘, ‘codecs‘, ‘idlelib.MultiCall‘, ‘operator‘, ‘idlelib.tabbedpages‘, ‘encodings.utf_8‘, ‘genericpath‘,
‘_weakrefset‘, ‘socket‘, ‘sre_constants‘, ‘sre_compile‘, ‘configparser‘, ‘signal‘, ‘errno‘, ‘_string‘, ‘shutil‘, ‘socketserver‘, ‘idlelib‘, ‘_codecs_cn‘,
‘idlelib.macosxSupport‘, ‘getopt‘, ‘idlelib.CallTips‘, ‘__future__‘, ‘dis‘, ‘_functools‘, ‘idlelib.ZoomHeight‘, ‘urllib.parse‘, ‘zipimport‘, ‘re‘,
‘_hashlib‘, ‘_bz2‘, ‘idlelib.UndoDelegator‘, ‘time‘, ‘_sitebuiltins‘, ‘idlelib.StackViewer‘, ‘tkinter.simpledialog‘, ‘opcode‘, ‘idlelib.keybindingDialog‘,
‘_multibytecodec‘, ‘_pickle‘, ‘functools‘, ‘msvcrt‘])
字节编译
>>.pyc文件
>>以.pyc为后缀的文件,经过编译后的python模块对应的二进制文件
>>字节编译与编译的区别
>>字节编译:将普通文件转化为二进制的过程,由解释器完成
>>编译:是指软件中有一个独立的编译模块去将程序编译
>>.pyc文件的产生
>>执行python模块.py文件时(import 模块名),如果没有对应的.pyc文件,系统会自动编译.py文件生成.pyc文件
>>在windows命令窗口输入:python -m compileall .py文件名
>>.pyc文件的使用
>>在python中,.pyc文件最大的作用就是加快运行速度
>>可进行反编译
from...import
>>使用from...import语句
>>导入模块中的指定方法
from 模块名 import 方法名
>>> #查看版本信息
>>> from sys import version
>>> version
‘3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]‘
>>使用from...import * 语句
>>将模块中的所有方法全部导入
>>> from sys import *
>>> #查看版本信息
>>> version
‘3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]‘
>>> #查看当前执行文件目录
>>> executable
‘C:\\Python35\\pythonw.exe‘
__name__属性
>>主模块
>>一个被直接使用而没有被别人调用的模块
print(__name__)
if __name__ == ‘__main__‘:
print(‘yes‘)
else:
print(‘no‘)
yes
>>一个模块的__name__属性的值是__main__,这个模块就是主模块
自定义模块
>>需要自己定义与编写的模块
>>将python程序保存在lib文件夹下,就形成了模块
dir()函数
>>dir()函数:查看指定模块功能列表
>>> import sys >>> dir(sys) [‘__displayhook__‘, ‘__doc__‘, ‘__excepthook__‘, ‘__interactivehook__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘__stderr__‘, ‘__stdin__‘,
‘__stdout__‘, ‘_clear_type_cache‘, ‘_current_frames‘, ‘_debugmallocstats‘, ‘_getframe‘, ‘_home‘, ‘_mercurial‘, ‘_xoptions‘, ‘api_version‘, ‘argv‘,
‘base_exec_prefix‘, ‘base_prefix‘, ‘builtin_module_names‘, ‘byteorder‘, ‘call_tracing‘, ‘callstats‘, ‘copyright‘, ‘displayhook‘, ‘dllhandle‘,
‘dont_write_bytecode‘, ‘exc_info‘, ‘excepthook‘, ‘exec_prefix‘, ‘executable‘, ‘exit‘, ‘flags‘, ‘float_info‘, ‘float_repr_style‘, ‘get_coroutine_wrapper‘,
‘getallocatedblocks‘, ‘getcheckinterval‘, ‘getdefaultencoding‘, ‘getfilesystemencoding‘, ‘getprofile‘, ‘getrecursionlimit‘, ‘getrefcount‘, ‘getsizeof‘,
‘getswitchinterval‘, ‘gettrace‘, ‘getwindowsversion‘, ‘hash_info‘, ‘hexversion‘, ‘implementation‘, ‘int_info‘, ‘intern‘, ‘is_finalizing‘,
‘last_traceback‘, ‘last_type‘, ‘last_value‘, ‘maxsize‘, ‘maxunicode‘, ‘meta_path‘, ‘modules‘, ‘path‘, ‘path_hooks‘, ‘path_importer_cache‘, ‘platform‘,
‘prefix‘, ‘set_coroutine_wrapper‘, ‘setcheckinterval‘, ‘setprofile‘, ‘setrecursionlimit‘, ‘setswitchinterval‘, ‘settrace‘, ‘stderr‘, ‘stdin‘, ‘stdout‘,
‘thread_info‘, ‘version‘, ‘version_info‘, ‘warnoptions‘, ‘winver‘]
>>dir()扩展
>>> a = [] >>> dir(a) [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘,
‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘,
‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘,
‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
>>可返回指定对象属性的列表
python模块
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。