10 python基础--模块

10.1 定义

模块: 定义的函数和变量的文件,其后缀名是.py,可被别的程序引入

10.2. 解析器搜索顺序是

1、当前目录
2、如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。
3、如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。

10.3. 导入模块

import sys   ##导入模块
from sys import argv,path    ##导入特定的成员
print('================python from import=================')
print('path:',path)     ## 因为已经导入path成员,所以此处引用时不需要加sys.path

10.4、dir()函数

dir() 可以找到模块内定义的所有名称,以一个字符串列表的形式返回
import sys
print(dir(sys))
>['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_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_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']

10.5、__name__属性

属性: 一个模块被另一个程序第一次引入时,其主程序将运行。__name__属性来使该程序块仅在该模块自身运行时执行
if __name__ =='__main__':
    print('程序自身运行')
else:
    print('我来自另一模块')

猜你喜欢

转载自blog.csdn.net/qq_25672165/article/details/85059324