programing Python --Sys module

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

 1 ‘‘‘
 2 aplit and interactively page a string of file of text
 3 ‘‘‘
 4
 5 def more(text,numlines= 15):
 6     lines = str.splitlines(text)
 7     while lines:
 8         chunk = lines[:numlines]
 9         lines = lines[numlines:]
10         for line in chunk:print(line)
11         if lines and raw_input(‘more?‘) not in [‘y‘,‘Y‘]:break
12
13
14 if __name__ == "__main__":
15     import sys
16     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

1 from more import more
2 import sys
3 more(sys.__doc__)

Introducing the sys Module

   PlatForms and Versions

1 if sys.platform[:3] == ‘win‘:print (‘hello windows‘)
2 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

1 try:
2     raise IndexError
3 except:
4     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

时间: 2024-08-26 06:28:32

programing Python --Sys module的相关文章

python Sys module

------------------------------------------------------------------------------------------------------ sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. ---------------------------------------------------

[Python]attributeError:'module' object has no attribute 'dump'

[问题] [代码] 文件名:pickle.py # coding=utf-8 #持久存储 import pickle #b 以二进制的模式打开文件 with open('mydata.pickle','wb') as mysavedata: #用dump保存数据 pickle.dump([1,2,'three'],mysavedata) #b 以二进制的模式打开文件 with open('mydata.pickle','rb') as myreaddata: #使用load恢复数据 list =

anaconda python no module named 'past'的解决方法

如上图所示,错误就是:No module named 'past' 解决办法不是下载'past'包,而是下载'future'包: 我是安装了anaconda集成环境,python的单独环境应该也是同样的,下面以anaconda安装 'future'包为例,命令是" pip install future",如下图: 成功安装即可解决这个问题(? ω ?) anaconda python no module named 'past'的解决方法

Python sys.setdefaultencoding('utf-8') 后就没输出

为了解决Python的 UnicodeDecodeError: 'ascii' codec can't decode byte ,我们可以加入以下代码. import sys reload(sys) sys.setdefaultencoding('utf-8') 但是在编辑的时候发现,普通的输出却不见了,如图 print 1都没反应. 查资料后解决,原来reload(sys)的时候,sys.stdout 这个参数被重置为了ipython 的对象,导致无法输出.因此可以用以下代码代替 import

Python Keras module 'keras.backend' has no attribute 'image_data_format'

问题: 当使用Keras运行示例程序mnist_cnn时,出现如下错误: 'keras.backend' has no attribute 'image_data_format' 程序路径https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py 使用的python conda环境是udacity自动驾驶课程的carnd-term1 故障程序段: if K.image_data_format() == 'channels

python sys.path.append

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

python 报错——Python TypeError: 'module' object is not callable 原因分析

原因分析:Python导入模块的方法有两种: import module 和 from module import 区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要 例: >>>import pprint >>>pprint.pprint(people) OR >>>from pprint import * >>>pprint(people) 正确的代码:>>> import Person>&g

Python “No module named”

Python "No module named" 例如有这样的一个包和它的模块: Test __init__.py Module01.py 当: from Test import Module01 或者 import Test.Module01 出现错误:No module named xxxx 的时候 如果命名拼写没有错,一般是你的 Test 包或者模块和其它 path 路径下的包或者模块同名了 可以: import Test print(Test.__path__) 查看 Test

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

操作系统:Ubuntu 问题:创建虚拟环境时,出现:/usr/bin/python: No module named virtualenvwrapper 解决方法: 1.切换到用户家目录 2.打开隐藏文件 .bashrc  vim .bashrc 3.在文件末尾添加 export WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenvexport VIRTUALENVW