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,然后是一些模块文件和 ...
随机推荐
- ansible-playbook流程控制-loops循环使用
1. ansible-playbook流程控制-loops循环使用 有时你想要多次重复任务.在计算机编程中,这称为循环.common ansible循环包括使用文件模块更改多个文件和/或目录的所 ...
- 大白话讲解 Java程序的运行机制和JVM
据我们所知,Java程序是跨平台的.那么Java是如何实现跨平台的呢?看完下面几句话就会恍然大悟! 1.为什么Java语言既是编译型语言又是解释型语言呢? 答:运行Java程序,首先需要经过编译,编译 ...
- [C++]invalid initialization of non-const reference of type 'std::__cxx11::string& {aka std::__cxx11::basi
解决方法:在参数前面加一个cosnt或者把引用符号去掉
- 多事之秋-最近在阿里云上遇到的问题:负载均衡失灵、服务器 CPU 100%、被 DDoS 攻击
昨天 22:00~22:30 左右与 23:30~00:30 左右,有1台服役多年的阿里云负载均衡突然失灵,造成通过这台负载均衡访问博客站点的用户遭遇 502, 503, 504 ,由此给您带来麻烦, ...
- Hibernate4之JPA规范配置详解
@Table Table用来定义entity主表的name,catalog,schema等属性. 属性说明: name:表名 catalog:对应关系数据库中的catalog schema:对应关系数 ...
- Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码
在文章:Mybatis源码解析,一步一步从浅入深(一):创建准备工程,中我们为了解析mybatis源码创建了一个mybatis的简单工程(源码已上传github,链接在文章末尾),并实现了一个查询功能 ...
- java数据结构——队列、循环队列(Queue)
每天进步一点点,坚持就是成功. 1.队列 /** * 人无完人,如有bug,还请斧正 * 继续学习Java数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...
- Player的跟踪狂 -- Camera
P.S.很多游戏里的Player都会设置的被跟踪,是人性的扭曲,还是XXX,正在解密. 第三人称视角 camera紧跟player背后(角度随player改变) using System.Collec ...
- spring系列常用注解
常见注解使用 - @SpringBootApplication,springboot的核心注解,用于开启自动配置,等效于@Configuraion.@ComponentScan和@EnableAuto ...
- Nginx 配置项优化详解
(1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...