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. Excel计算一列的和sum(A:A)

    在公式中输入=sum(A2:A6),计算的是A列2-6行的和 =sum(A:A)计算的是A列全部的和

  2. CSS伪类

    CSS伪类:控制元素的某种状态 语法:元素名称:伪类名称{属性:值} CSS伪类控制链接状态 状态 语法 未访问的链接 a:link{color:#ff00ff} 已访问的链接 a:visited{c ...

  3. Redis内存管理(一)

    Redis数据库的内存管理函数有关的文件为:zmalloc.h和zmalloc.c. Redis作者在编写内存管理模块时考虑到了查看系统内是否安装了TCMalloc或者Jemalloc模块,这两个是已 ...

  4. SQL存储过程大全

    --增加 create proc usp_insertToText ), ), @usitPrice decimal as begin insert into TEST1 output inserte ...

  5. Swift - 简单封装一个工具类模板

    创建模板类(封装一个类) 例1:新建一个名字叫做 Product 的类 Product.swift File 的内容 class Product { var name: String var desc ...

  6. rpm -qc 来查找安装包的配置文件

    rpm -qc elasticsearch /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/jvm.options /etc/elast ...

  7. C#关键字params

    using System; using System.Threading; namespace Test { /// <summary> /// params用法: 1.用来修饰方法的参数 ...

  8. Delphi的面向对象编程基础笔记

    1.面向对象.一门面向对象的编程语言至少要实现以下三个OOP的概念 封装:把相关的数据和代码结合在一起,并隐藏细节.封装的好处是利用程序的模块化,并把代码和其他代码分开 继承:是指一个新的类能够从父类 ...

  9. Pyqt QTabWidget 简单的计算器集合

    今天我们简单介绍下QTabWidget,然后在加入Demo计算器 首先我先讲下文件的结构: 文件分四部分, 一部分是Ui设计文件, 一部分是由Ui生成的py文件, 一部分是 计算器的逻辑文件,  最后 ...

  10. 【计算机图形学】openGL常用函数

    OpenGL常用函数   glAccum 操作累加缓冲区   glAddSwapHintRectWIN 定义一组被 SwapBuffers拷贝的三角形   glAlphaFunc允许设置alpha检测 ...