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__

python学习-模块与包(九)的更多相关文章

  1. python学习——模块和包

    在之前常用模块中我们已经初步了解了模块的导入,今天来说学习一下模块和包.我们可以把模块理解成每一个python文件.而包就是多个能解决一类问题的python文件全部放在一起.OK

  2. Python之模块和包导入

    Python之模块和包导入 模块导入: 1.创建名称空间,用来存放模块XX.py中定义的名字 2.基于创建的名称空间来执行XX.py. 3.创建名字XX.py指向该名称空间,XX.名字的操作,都是以X ...

  3. 一文搞懂 Python 的模块和包,在实战中的最佳实践

    最近公司有个项目,我需要写个小爬虫,将爬取到的数据进行统计分析.首先确定用 Python 写,其次不想用 Scrapy,因为要爬取的数据量和频率都不高,没必要上爬虫框架.于是,就自己搭了一个项目,通过 ...

  4. Python之模块和包学习

    模块简介 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某个模块中的函数或对象 ...

  5. selenium + python自动化测试unittest框架学习(四)python导入模块及包知识点

    在写脚本的时候,发现导入某些模块,经常报错提示导入模块失败,这里来恶补下python导入模块的知识点. 1.模块导入时文件查找顺序 在脚本中,import xxx模块时的具体步骤: (1)新建一个mo ...

  6. python基础-------模块与包(一)

    模块与包 Python中的py文件我们拿来调用的为之模块:主要有内置模块(Python解释器自带),第三方模块(别的开发者开发的),自定义模块. 目前我们学习的是内置模块与第三方模块. 通过impor ...

  7. (Python )模块、包

    本节开始学习模块的相关知识,主要包括模块的编译,模块的搜索路径.包等知识 1.模块 如果我们直接在解释器中编写python,当我们关掉解释器后,再进去.我们之前编写的代码都丢失了.因此,我们需要将我们 ...

  8. python 浅析模块,包及其相关用法

    今天买了一本关于模块的书,说实话,模块真的太多了,小编许多也不知道,要是把模块全讲完,可能得出本书了,所以小编在自己有限的能力范围内在这里浅析一下自己的见解,同时讲讲几个常用的模块. 这里是2018. ...

  9. python中模块、包、库的区别和使用

    模块:就是.py文件,里面定义了一些函数和变量,需要的时候就可以导入这些模块. 包:在模块之上的概念,为了方便管理而将文件进行打包.包目录下第一个文件便是 __init__.py,然后是一些模块文件和 ...

随机推荐

  1. pip安装路径

    pip show 模块名称 即可查看pip安装的包所在路径. 如numpy: pip show numpy

  2. Node.js之模块机制

    > 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. ![file](https://img2018.cnblogs.com/blog/830272/20 ...

  3. django配置静态文件的两种方法

    方法一:按照django配置静态文件的方法,可以在APP应用目录下创建一个static的文件夹,然后在static文件夹下创建一个和APP同名的文件夹,如我有一个blog的django项目,在下面有一 ...

  4. Vue学习之如何进行调试

    调试方法 vue调式方法,浏览器检查元素进入到console 1.console.log() 2.alert('sd') 3.debugger //程序会运行到这里停止 ![](https://img ...

  5. vue把链接转二维码

    使用qrcodejs2插件 1. 安装qrcodejs2:npm install qrcodejs2 --save 2. 在组件里面引入: import QRCode from 'qrcodejs2' ...

  6. 基于Spark的电影推荐系统(电影网站)

    第一部分-电影网站: 软件架构: SpringBoot+Mybatis+JSP 项目描述:主要实现电影网站的展现 和 用户的所有动作的地方 技术选型: 技术 名称 官网 Spring Boot 容器 ...

  7. 学习WEBAPI第一天

    目录 WebApi: 通过操作对象来实现操作标签的目的 一.DOM 中常用的操作 二.doucument对象 三.获取元素 四.注册事件 五.操作元素的属性 六.当页面加载完时,script代码已经执 ...

  8. Kubernetes 系列(六):Kubernetes部署Prometheus监控

    1.创建命名空间 新建一个yaml文件命名为monitor-namespace.yaml,写入如下内容: apiVersion: v1 kind: Namespace metadata: name: ...

  9. 最近太多人问Protobuf的问题了,把这个重新搬出来!

    pb杀手 我先让pbkiller做个自我介绍 pbkiller:我是一位专业的争对 protobuf 问题训练有素的杀手,我可以为您轻松搞定 protobuf 在 Cocos Creaotr 开发中的 ...

  10. for循环用腻了,试试列表生成式。

    在编写程序或者查看别人的程序时,经常会遇到列表生成式,这个使用起来并不复杂,但是非常有用,使我们的代码更加简洁灵活.很多python使用者并不太会使用它.今天,就给大家详细讲解列表生成式和生成器表达式 ...