zoukankan      html  css  js  c++  java
  • python学习-模块与包(九)

    9.2加载模块

    import sys
    from pprint import pprint
    pprint(sys.path)
    
    输出结果:
    ['D:\myproject\python_lxf',
     'D:\myproject\python_lxf',
     'D:\soft\PyCharm 2019.3.3\plugins\python\helpers\pycharm_display',
     'D:\soft\python36\python36.zip',
     'D:\soft\python36\DLLs',
     'D:\soft\python36\lib',
     'D:\soft\python36',
     'D:\soft\python36\lib\site-packages',
     'D:\soft\python36\lib\site-packages\geventhttpclient_wheels-1.3.1.dev2-py3.6-win-amd64.egg',
     'D:\soft\python36\lib\site-packages\win32',
     'D:\soft\python36\lib\site-packages\win32\lib',
     'D:\soft\python36\lib\site-packages\Pythonwin',
     'D:\soft\PyCharm '
     '2019.3.3\plugins\python\helpers\pycharm_matplotlib_backend']
    默认的模块加载路径
    >>> import sys
    >>> sys.path.append('/Users/michael/my_py_scripts')
    添加自己的搜索目录

    9.4查看模块内容

    dir(): 返回模块或类所包含的全部程序单元(包括变量、函数、类和方法等)

    __all__:模块本身提供的变量,不会展示以下划线开头的程序单元。另使用from xx import *,是不会导入以下划线开头的程序单元的

    import logging,pprint
    pprint.pprint(dir(logging))
    ['BASIC_FORMAT',
     'BufferingFormatter',
     'CRITICAL',
     'DEBUG',
     'ERROR',
     'FATAL',
     'FileHandler',
     'Filter',
     'Filterer',
     'Formatter',
     'Handler',
     'INFO',
     'LogRecord',
     'Logger',
     'LoggerAdapter',
     'Manager',
     'NOTSET',
     'NullHandler',
     'PercentStyle',
     'PlaceHolder',
     'RootLogger',
     'StrFormatStyle',
     'StreamHandler',
     'StringTemplateStyle',
     'Template',
     'WARN',
     'WARNING',
     '_STYLES',
     '_StderrHandler',
     '__all__',
     '__author__',
     '__builtins__',
     '__cached__',
     '__date__',
     '__doc__',
     '__file__',
     '__loader__',
     '__name__',
     '__package__',
     '__path__',
     '__spec__',
     '__status__',
     '__version__',
     '_acquireLock',
     '_addHandlerRef',
     '_checkLevel',
     '_defaultFormatter',
     '_defaultLastResort',
     '_handlerList',
     '_handlers',
     '_levelToName',
     '_lock',
     '_logRecordFactory',
     '_loggerClass',
     '_nameToLevel',
     '_releaseLock',
     '_removeHandlerRef',
     '_showwarning',
     '_srcfile',
     '_startTime',
     '_warnings_showwarning',
     'addLevelName',
     'atexit',
     'basicConfig',
     'captureWarnings',
     'collections',
     'critical',
     'currentframe',
     'debug',
     'disable',
     'error',
     'exception',
     'fatal',
     'getLevelName',
     'getLogRecordFactory',
     'getLogger',
     'getLoggerClass',
     'info',
     'io',
     'lastResort',
     'log',
     'logMultiprocessing',
     'logProcesses',
     'logThreads',
     'makeLogRecord',
     'os',
     'raiseExceptions',
     'root',
     'setLogRecordFactory',
     'setLoggerClass',
     'shutdown',
     'sys',
     'threading',
     'time',
     'traceback',
     'warn',
     'warning',
     'warnings',
     'weakref']
    pprint.pprint(logging.__all__)
    ['BASIC_FORMAT',
     'BufferingFormatter',
     'CRITICAL',
     'DEBUG',
     'ERROR',
     'FATAL',
     'FileHandler',
     'Filter',
     'Formatter',
     'Handler',
     'INFO',
     'LogRecord',
     'Logger',
     'LoggerAdapter',
     'NOTSET',
     'NullHandler',
     'StreamHandler',
     'WARN',
     'WARNING',
     'addLevelName',
     'basicConfig',
     'captureWarnings',
     'critical',
     'debug',
     'disable',
     'error',
     'exception',
     'fatal',
     'getLevelName',
     'getLogger',
     'getLoggerClass',
     'info',
     'log',
     'makeLogRecord',
     'setLoggerClass',
     'shutdown',
     'warn',
     'warning',
     'getLogRecordFactory',
     'setLogRecordFactory',
     'lastResort',
     'raiseExceptions']
    dir()、__all__

    使用__doc__属性查看文档。另:使用help()函数查看的其实就是程序单元的__doc__属性值

    import string
    help(string.capwords)
    Help on function capwords in module string:
    capwords(s, sep=None)
        capwords(s [,sep]) -> string
        
        Split the argument into words using split, capitalize each
        word using capitalize, and join the capitalized words using
        join.  If the optional second argument sep is absent or None,
        runs of whitespace characters are replaced by a single space
        and leading and trailing whitespace are removed, otherwise
        sep is used to split and join the words.
    print(string.capwords.__doc__)
    capwords(s [,sep]) -> string
        Split the argument into words using split, capitalize each
        word using capitalize, and join the capitalized words using
        join.  If the optional second argument sep is absent or None,
        runs of whitespace characters are replaced by a single space
        and leading and trailing whitespace are removed, otherwise
        sep is used to split and join the words.
    __doc__与help()

    使用__file__属性查看模块的源文件路径

    import string
    string.__file__
    'E:\soft\Python\Python36\lib\string.py'
    __file__
  • 相关阅读:
    [转]pycharm的一些快捷键
    Python学习笔记(三十五)—内置模块(4)struct
    Python学习笔记(三十四)—内置模块(3)base64
    Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter
    Python学习笔记(三十二)常用内建模块(1)— datetime
    Python学习笔记(二十九)ThreadLocal
    使用Java代码发送SMTP邮件
    Python学习笔记(二十五)操作文件和目录
    Python学习笔记(二十)调试
    Python学习笔记(十八)@property
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11520564.html
Copyright © 2011-2022 走看看