Recall that every python module has a built_in __name__ variable that python sets to the __main__ string only when the file is run as a program,not when it's imported as library

so in the python fiel if __name__ == "__main__" : .... is the top-level code

 '''
aplit and interactively page a string of file of text
''' def more(text,numlines= 15):
lines = str.splitlines(text)
while lines:
chunk = lines[:numlines]
lines = lines[numlines:]
for line in chunk:print(line)
if lines and raw_input('more?') not in ['y','Y']:break if __name__ == "__main__":
import sys
more(open(sys.argv[0]).read(),10)

when the more.py file is imported ,we pass an explicit string to its more function,so this is the imported way to run the more function

 from more import more
import sys
more(sys.__doc__)

Introducing the sys Module

   PlatForms and Versions

 if sys.platform[:3] == 'win':print ('hello windows')
elif sys.platform[:3] == 'lin':print ('hello linux')

If you have code that must act differently on different machines, simply test the sys.platform string as done,

The Module Search Path

sys.path is a list fo directory name strings representing the true search path in running python interpreter, when a module is imported python scans this list from left to right,searching for the module's file on each directory named in the list,

the sys.path list in simply initialized from your PYTHONPATH setting

Surprisingly,sys.path can actually be changed by program. using apend,extend,insert,pop,remove function

The Loaded Modules Table

sys.modules is a dictionary containing one name:module entry for every module imported in your python session

>>> sys.modules

Excepstion Detials

  other attribures in the sys module allow us to fetch all the information related to the most recently python exception,the sys.exc_info function returns a tuple with the latest exception's type,value and traceback object

 try:
raise IndexError
except:
print (sys.exc_info())

Other sys Module Exports:

  Command_line arguments show up as a list of string called sys.argv

  Standard streams are available as sys.stdin sys.stdout and sys.stderr

program exit can be forced with sys.exit calls

programing Python --Sys module的更多相关文章

  1. python no module named _socket 原因

    python no module named _socket 原因 Lib/site-packages 不在 sys.path 中

  2. python sys.modules模块

    sys.modules是一个全局字典,该字典是python启动后就加载在内存中.每当程序员导入新的模块,sys.modules都将记录这些模块.字典sys.modules对于加载模块起到了缓冲的作用. ...

  3. python模块module package

    python模块module   package module package 常用模块 模块与包的区别 模块分为内置模块.第三方模块,自定义模块 程序会先从内置到第三方再到当前工作目录下去找你导入的 ...

  4. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  5. Python sys.path.append

    python sys.path.append 对于模块和自己写的程序不在同一个目录下,可以把模块的路径通过sys.path.append(路径)添加到程序中. 在程序开头加上: import syss ...

  6. Python——sys模块

    七.sys模块 sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传递参数. sys.exit([arg]): 程序中间的退出,arg=0为正常退出. sys.getdefaulten ...

  7. Python “No module named” 以及在Python2中可以导入,但在python3中却出现的原因

    Python “No module named” 以及在Python2中可以导入,但在python3中却出现的原因 原因之1: 例如有这样的一个包和它的模块: Test __init__.py Mod ...

  8. python sys模块(12)

    在python sys模块提供对解释器使用或维护的一些变量的访问,以及与解释器强烈交互的函数!关于sys模块在官网也有详细的介绍:python sys模块官方介绍. 一.sys模块简介 sys.arg ...

  9. 问题1-/usr/bin/python: No module named virtualenvwrapper

    操作系统:Ubuntu 问题:创建虚拟环境时,出现:/usr/bin/python: No module named virtualenvwrapper 解决方法: 1.切换到用户家目录 2.打开隐藏 ...

随机推荐

  1. 页面上有两个元素id相同,js中如何取值

    页面上有两个table,id都是”cont2",现要在js中取到这两个table,改变样式. js实现: var tab2=document.all.cont2(1);var  tab=do ...

  2. iOS 日常工作之常用宏定义大全

    转自:http://www.jianshu.com/p/213b3b96cafe 前言: 在工作中, 很多小伙伴都会在PCH文件定义一些常用的宏,但是又怕写这些简单的宏浪费时间,又有时候忘记怎么定义了 ...

  3. VS2010编译Qt4.8.2的64版本库

    安装qt-win-opensource-4.8.2-vs2010.exe(从http://download.qt.io/archive/qt/4.8/4.8.2/下 载),这个是32位的,里面有编译好 ...

  4. ASP.NET 下载文件并继续执行JS解决方法

    需求说明:当用户点击按钮时使当前按钮为不可用,并打开新页面,关闭新页面时,按钮变为可用.并且如果不关闭新页面,当前按钮过10秒钟自动变为可用. 包含3个页面: 一.按钮页 前台代码:当刷新后采用js进 ...

  5. osgi学习

    Bundle可以被动态地安装.启动.停止和卸载.Bundle是服务(Service)和组件(Component)的载体.在OSGi中,每个Bundle都有自己独立于其他Bundle的ClassLoad ...

  6. Struts2拦截器之ModelDrivenInterceptor

    叙述套路: 1.这是个啥东西,它是干嘛用的? 2.我知道它能干啥了,那它咋个用呢? 3.它能跑起来了,但是它是咋跑起来的是啥原理呢? 一.ModelDriven是个啥?他能做什么? 从前端页面到后端的 ...

  7. NYOJ之Fibonacci数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAJwCAIAAAD0kmsHAAAgAElEQVR4nO3dvXLbOMM24O8k3PtA3E

  8. php基础面试题1

    问题1:谈谈你对的PHP的基本认识. 回答:PHP是Hypertext Preprocessor(超文本预处理器)的简称,是一种用来开发动态网站的服务器端脚本语言. 问题2:什么是MVC? 回答:MV ...

  9. Linux SNMP oid

    http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html

  10. oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法

    最近使用oraclede impdp工具全库导入数据库时,在数据库里面使用出现如下情况. SQL state : 违反唯一约束条件 (GDXAORCL.SYS_C0055359) ; nested e ...