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__
python学习-模块与包(九)的更多相关文章
- python学习——模块和包
在之前常用模块中我们已经初步了解了模块的导入,今天来说学习一下模块和包.我们可以把模块理解成每一个python文件.而包就是多个能解决一类问题的python文件全部放在一起.OK
- Python之模块和包导入
Python之模块和包导入 模块导入: 1.创建名称空间,用来存放模块XX.py中定义的名字 2.基于创建的名称空间来执行XX.py. 3.创建名字XX.py指向该名称空间,XX.名字的操作,都是以X ...
- 一文搞懂 Python 的模块和包,在实战中的最佳实践
最近公司有个项目,我需要写个小爬虫,将爬取到的数据进行统计分析.首先确定用 Python 写,其次不想用 Scrapy,因为要爬取的数据量和频率都不高,没必要上爬虫框架.于是,就自己搭了一个项目,通过 ...
- Python之模块和包学习
模块简介 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某个模块中的函数或对象 ...
- selenium + python自动化测试unittest框架学习(四)python导入模块及包知识点
在写脚本的时候,发现导入某些模块,经常报错提示导入模块失败,这里来恶补下python导入模块的知识点. 1.模块导入时文件查找顺序 在脚本中,import xxx模块时的具体步骤: (1)新建一个mo ...
- python基础-------模块与包(一)
模块与包 Python中的py文件我们拿来调用的为之模块:主要有内置模块(Python解释器自带),第三方模块(别的开发者开发的),自定义模块. 目前我们学习的是内置模块与第三方模块. 通过impor ...
- (Python )模块、包
本节开始学习模块的相关知识,主要包括模块的编译,模块的搜索路径.包等知识 1.模块 如果我们直接在解释器中编写python,当我们关掉解释器后,再进去.我们之前编写的代码都丢失了.因此,我们需要将我们 ...
- python 浅析模块,包及其相关用法
今天买了一本关于模块的书,说实话,模块真的太多了,小编许多也不知道,要是把模块全讲完,可能得出本书了,所以小编在自己有限的能力范围内在这里浅析一下自己的见解,同时讲讲几个常用的模块. 这里是2018. ...
- python中模块、包、库的区别和使用
模块:就是.py文件,里面定义了一些函数和变量,需要的时候就可以导入这些模块. 包:在模块之上的概念,为了方便管理而将文件进行打包.包目录下第一个文件便是 __init__.py,然后是一些模块文件和 ...
随机推荐
- html中的空格
网上摘录: HTML提供了6种空格实体.除第一种外,其他几种空格在不同浏览器中宽度各异. 它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格, ...
- asp.net 开源工作流-流程属性-流程关键字段
关键词:工作流快速开发平台 工作流流设计 业务流程管理 Java工作流引擎 asp.net 开源工作流 net开源工作流引擎 开源工作流系统 定义:业务关键字段也叫流程实例的摘要字段,他提取流程 ...
- 转:LinkedHashMap和HashMap的比较使用
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.uti ...
- [python]python的异常处理
异常处理:首先了解异常,程序出现逻辑错误或者用户输入不合法都会引发异常,而这些异常并不是致命的所以不会导致程序崩溃死掉.可以利用Python提供的异常处理机制在异常出现时及时捕获,并从内部自我消化. ...
- Android Studio [Activity的生命周期]
package com.xdw.a122; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; imp ...
- CentOS7 搭建php环境
1,先安装apache: yum install httpd 配置ServerName,进入httpd.conf文件: vi /etc/httpd/conf/httpd.conf 将#ServerNa ...
- 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(线程池版)
# 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from queue import Queue import requests from lxml import etree ...
- 从零开始入门 K8s | 应用存储和持久化数据卷:存储快照与拓扑调度
作者 | 至天 阿里巴巴高级研发工程师 一.基本知识 存储快照产生背景 在使用存储时,为了提高数据操作的容错性,我们通常有需要对线上数据进行 snapshot ,以及能快速 restore 的能力.另 ...
- Java 学习笔记之 线程Yield
线程Yield: yield()方法的作用是放弃当前的CPU资源,将它让给其他的任务去占用CPU执行时间,但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片. public class Yie ...
- 网页布局——Flex弹性框布局
布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 需要安卓4.4及以上版本可以使用 ...