python——sys模块
作者:互联网
sys.argv
功能:在外部向程序内部传递参数
示例:sys.py
import sys
print sys.argv[0]
print sys.argv[1]
运行:
>>>python sys.py argv1
sys.py
argv1
sys.exit(n)
功能:执行到主程序末尾,解释器自动退出,但是如果需要中途退出程序,可以调用sys.exit函数,带有一个可选的整数参数返回给调用它的程序,表示你可以在主程序中捕获对sys.exit的调用。(0是正常退出,其他为异常)
示例:exit.py
#!/usr/bin/env python
import sys
def exitfunc(value):
print value
sys.exit(0)
print "hello"
try:
sys.exit(1)
except SystemExit,value:
exitfunc(value)
print "come?"
运行:
>>> python exit.py
hello
1
sys.exitfunc (回调函数)
通过sys.exitfunc来注册程序退出时的回调函数,我们可以在这个回调函数中做一些资源清理的操作,
但通过它只能注册一个回调,而且还不支持参数(可以带默认参数)。
【1】exit_handler.py文件:
#!usr/bin/env python
#coding:utf-8
import sys
def exitfunc():
print 'exit is done!'
sys.exitfunc=exitfunc #注册回调函数
if __name__=='__main__':
print 'hello world'
#程序运行结束,在退出解释器之前,会自动调用回调函数。。
>>> python exit_handler.py
hello world
exit is done!
【2】exit_handler1.py文件:
#!usr/bin/env python
#coding:utf-8
import sys
def exitfunc():
print 'exit is done!'
sys.exitfunc=exitfunc #通过sys.exitfunc来注册程序退出时的回调函数
print 'hello world'
sys.exit(1)#执行至主程序的末尾时,解释器会自动退出。 我们也可以调用sys.exit函数使程序中途退出解释器!
print 'ok'#该语句不执行
>>> python exit_handler1.py
hello world
exit is done!
【3】exit_handler2.py文件:
#!usr/bin/env python
#coding:utf-8
import sys
def exitfunc():
print 'exit is done!'
sys.exitfunc=exitfunc
if __name__=='__main__':
import os
print '回调函数没有执行!'
os._exit(0) #通过os._exit()退出,注册的回调函数将不会被调用
>>> python exit_handler3.py
回调函数没有执行!
通过os._exit()退出,注册的回调函数将不会被调用
关于回调函数,sys.exitfunc有其自身的缺点,比如,只能注册一个回调函数,不能传递参数。。。为了更加方便的注册回调,我们一般使用atexit模块。。。
关于atexit模块,请参考:点击打开链接
sys.path
本着下定义开头吧:python中import某个A模块时,首先会从python的内置模块中查找是否含义该模块的定义若未查询到会从sys.path对应的模块路径查询是否含有对应模块的定义,如果搜索完成依然没有对应A模块时则抛出import的异常
接着说明下python的两种加载py文件的方式:
- python xxx.py
- python -m xxx.py
第一种方式是直接运行方式
第二种方式是把模块当做脚本来启动
可能看起来说的python运行方式和sys.path有点大相径庭,但实际上两种不同方式的运行导致sys.path[0]的值是有差异的
第一种方式:sys.path[0]是当前脚本的运行目录
第二种方式:sys.path[0]是空值字符串,也就是当前执行python的目录
sys.path是一个python搜索模块的路径列表:
eg、下边的X.py文件中打印出sys.path内容:
import sys
print sys.path
python X.py运行
其中sys.path[0]是 I:\restful_code\tester,对应调用python解释器的脚本所在的目录。 其实就是存放需要运行的代码的路径
python -m x.py运行
使用场景:
在实际开发中,默认包含了当前目录为搜索路径,所以,当前目录下的模块和子模块均可以正常访问。但是若一个模块需要import平级的不同目录的模块,或者上级目录里面的模块,就可以通过修改path来实现
修改path的两种方法:
方法一:函数添加
这是即时生效的方法,就是在模块里面修改sys.path值,这种方法修改的sys.path作用域只是当前进程,进程结束后就失效了。
个人比较推荐这种方法,比较干净, 避免一些冲突问题。
比如现在的代码目录结构:
/src/configs/config.py
/src/common/Database.py
假如Database.py期望导入config. py,则可以增加上级目录到sys.path列表里面:
parent_path = os.path.dirname(sys.path[0])
if parent_path not in sys.path:
sys.path.append(parent_path)
import configs.config
方法一:修改环境变量
添加系统环境变量PYTHONPATH,在这个环境变量中输入相关的路径,不同的路径之间用逗号(英文的!)分开。路径会自动加入到sys.path中。
os.path.abspath(__file__) 作用: 获取当前脚本的完整路径
sys.path.append(’引用模块的地址')对于模块和自己写的程序不在同一个目录下,
可以把模块的路径通过sys.path.append(路径)添加到程序中。
sys.modules
sys.modules是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对于加载模块起到了缓冲的作用。当某个模块第一次导入,字典sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。
字典sys.modules具有字典所拥有的一切方法,可以通过这些方法了解当前的环境加载了哪些模块
import os
import shutil
import sys
print sys.modules
{'code': <module 'code' from 'D:\ana\envs\py27\lib\code.pyc'>, 'numpy.core.info': <module 'numpy.core.info' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\info.pyc'>, '_pydevd_bundle.code': None, 'ctypes.os': None, 'gc': <module 'gc' (built-in)>, '_pydevd_bundle.os': None, 'distutils.sysconfig': <module 'distutils.sysconfig' from 'D:\ana\envs\py27\lib\distutils\sysconfig.pyc'>, 'pkg_resources._vendor.traceback': None, 'email.warnings': None, 'pkg_resources._vendor.packaging.__about__': <module 'pkg_resources._vendor.packaging.__about__' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\__about__.pyc'>, '_pydev_bundle._pydev_completer': <module '_pydev_bundle._pydev_completer' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\_pydev_completer.py'>, 'pprint': <module 'pprint' from 'D:\ana\envs\py27\lib\pprint.pyc'>, 'pydevd_concurrency_analyser.pydevd_concurrency_logger': <module 'pydevd_concurrency_analyser.pydevd_concurrency_logger' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_concurrency_analyser\pydevd_concurrency_logger.py'>, 'unittest.sys': None, 'numpy.core.umath': <module 'numpy.core.umath' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\umath.pyc'>, 'numpy._pytesttester': <module 'numpy._pytesttester' from 'D:\ana\envs\py27\lib\site-packages\numpy\_pytesttester.pyc'>, 'string': <module 'string' from 'D:\ana\envs\py27\lib\string.pyc'>, 'SocketServer': <module 'SocketServer' from 'D:\ana\envs\py27\lib\SocketServer.pyc'>, 'numpy.lib.arraysetops': <module 'numpy.lib.arraysetops' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\arraysetops.pyc'>, 'pydevd_concurrency_analyser.time': None, 'numpy.core._multiarray_tests': <module 'numpy.core._multiarray_tests' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_multiarray_tests.pyd'>, '_pydevd_bundle.pydevd_comm_constants': <module '_pydevd_bundle.pydevd_comm_constants' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm_constants.py'>, 'email.quoprimime': <module 'email.quoprimime' from 'D:\ana\envs\py27\lib\email\quoprimime.pyc'>, 'json.encoder': <module 'json.encoder' from 'D:\ana\envs\py27\lib\json\encoder.pyc'>, 'subprocess': <module 'subprocess' from 'D:\ana\envs\py27\lib\subprocess.pyc'>, 'numpy.core.machar': <module 'numpy.core.machar' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\machar.pyc'>, '_pydevd_bundle.pydevd_dont_trace_files': <module '_pydevd_bundle.pydevd_dont_trace_files' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_dont_trace_files.py'>, 'unittest.StringIO': None, 'encodings._codecs_cn': None, 'email.codecs': None, 'numpy.ma.extras': <module 'numpy.ma.extras' from 'D:\ana\envs\py27\lib\site-packages\numpy\ma\extras.pyc'>, 'numpy.fft.fftpack_lite': <module 'numpy.fft.fftpack_lite' from 'D:\ana\envs\py27\lib\site-packages\numpy\fft\fftpack_lite.pyd'>, 'math': <module 'math' (built-in)>, 'dis': <module 'dis' from 'D:\ana\envs\py27\lib\dis.pyc'>, 'zlib': <module 'zlib' (built-in)>, '_pydevd_bundle.pydevd_cython_wrapper': <module '_pydevd_bundle.pydevd_cython_wrapper' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_cython_wrapper.py'>, 'pkg_resources._vendor.pprint': None, '_pydevd_bundle.pydevd_extension_api': <module '_pydevd_bundle.pydevd_extension_api' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_extension_api.py'>, '_pydev_bundle.pydev_log': <module '_pydev_bundle.pydev_log' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_log.py'>, 'numpy.core._ctypes': None, 'matplotlib.cbook.warnings': None, 'unittest.pprint': None, 'encodings.gbk': <module 'encodings.gbk' from 'D:\ana\envs\py27\lib\encodings\gbk.pyc'>, 'matplotlib.cbook.textwrap': None, 'abc': <module 'abc' from 'D:\ana\envs\py27\lib\abc.pyc'>, '_warnings': <module '_warnings' (built-in)>, 'pkg_resources._vendor.collections': None, 'numpy._globals': <module 'numpy._globals' from 'D:\ana\envs\py27\lib\site-packages\numpy\_globals.pyc'>, 'numpy.lib.npyio': <module 'numpy.lib.npyio' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\npyio.pyc'>, 'pkg_resources._vendor.copy': None, 'matplotlib.sys': None, 'ntpath': <module 'ntpath' from 'D:\ana\envs\py27\lib\ntpath.pyc'>, 'numpy.fft.helper': <module 'numpy.fft.helper' from 'D:\ana\envs\py27\lib\site-packages\numpy\fft\helper.pyc'>, 'pydev_ipython': <module 'pydev_ipython' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydev_ipython\__init__.py'>, 'unittest.suite': <module 'unittest.suite' from 'D:\ana\envs\py27\lib\unittest\suite.pyc'>, '_pydevd_bundle._pydev_imps': None, 'matplotlib': <module 'matplotlib' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\__init__.pyc'>, 'exceptions': <module 'exceptions' (built-in)>, 'json.scanner': <module 'json.scanner' from 'D:\ana\envs\py27\lib\json\scanner.pyc'>, 'codecs': <module 'codecs' from 'D:\ana\envs\py27\lib\codecs.pyc'>, 'numpy.os': None, '_pydevd_bundle.types': None, 'email.socket': None, 'StringIO': <module 'StringIO' from 'D:\ana\envs\py27\lib\StringIO.pyc'>, 'pkg_resources': <module 'pkg_resources' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\__init__.pyc'>, 'weakref': <module 'weakref' from 'D:\ana\envs\py27\lib\weakref.pyc'>, 'numpy.core._internal': <module 'numpy.core._internal' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_internal.pyc'>, '_pydevd_bundle': <module '_pydevd_bundle' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\__init__.py'>, 'distutils.sys': None, 'numpy.lib.arraypad': <module 'numpy.lib.arraypad' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\arraypad.pyc'>, '_pydevd_bundle.abc': None, '_pydevd_bundle.pydevd_plugin_utils': <module '_pydevd_bundle.pydevd_plugin_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_plugin_utils.py'>, 'base64': <module 'base64' from 'D:\ana\envs\py27\lib\base64.pyc'>, '_pydevd_bundle.linecache': None, '_json': <module '_json' (built-in)>, 'email.FeedParser': <email.LazyImporter object at 0x00000000134E9748>, 'pkg_resources._vendor.warnings': None, 'email.charset': <module 'email.charset' from 'D:\ana\envs\py27\lib\email\charset.pyc'>, 'pkg_resources._vendor.datetime': None, '_pydev_imps': <module '_pydev_imps' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\__init__.py'>, 'select': <module 'select' from 'D:\ana\envs\py27\DLLs\select.pyd'>, 'ctypes._ctypes': None, '_heapq': <module '_heapq' (built-in)>, 'six.moves.urllib': <module 'six.moves.urllib' (built-in)>, '_pydev_bundle._subprocess': None, 'numpy.lib.financial': <module 'numpy.lib.financial' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\financial.pyc'>, 'pkg_resources._vendor.packaging.specifiers': <module 'pkg_resources._vendor.packaging.specifiers' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\specifiers.pyc'>, 'binascii': <module 'binascii' (built-in)>, 'email.MIMEMessage': <email.LazyImporter object at 0x00000000134E7088>, 'email._parseaddr': <module 'email._parseaddr' from 'D:\ana\envs\py27\lib\email\_parseaddr.pyc'>, 'email.sys': None, 'tokenize': <module 'tokenize' from 'D:\ana\envs\py27\lib\tokenize.pyc'>, 'numpy.core.numpy': None, 'numpy.polynomial.chebyshev': <module 'numpy.polynomial.chebyshev' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\chebyshev.pyc'>, 'pkg_resources.extern.six.moves.urllib': <module 'pkg_resources._vendor.six.moves.urllib' (built-in)>, 'cPickle': <module 'cPickle' (built-in)>, 'numpy.polynomial.hermite_e': <module 'numpy.polynomial.hermite_e' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\hermite_e.pyc'>, '_pydevd_bundle.pydevd_signature': <module '_pydevd_bundle.pydevd_signature' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_signature.py'>, 'pydevd_concurrency_analyser.pydevd_thread_wrappers': <module 'pydevd_concurrency_analyser.pydevd_thread_wrappers' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_concurrency_analyser\pydevd_thread_wrappers.py'>, '_pydevd_bundle.pydevd_vars': <module '_pydevd_bundle.pydevd_vars' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_vars.py'>, 'pydevd_concurrency_analyser.pydevd_concurrency_analyser': None, '_pydevd_bundle.pydevd_traceproperty': <module '_pydevd_bundle.pydevd_traceproperty' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_traceproperty.py'>, '_pydevd_frame_eval.os': None, 'pydev_ipython.inputhook': <module 'pydev_ipython.inputhook' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydev_ipython\inputhook.py'>, 'numpy.core.fromnumeric': <module 'numpy.core.fromnumeric' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\fromnumeric.pyc'>, 'numpy.ctypeslib': <module 'numpy.ctypeslib' from 'D:\ana\envs\py27\lib\site-packages\numpy\ctypeslib.pyc'>, 'matplotlib._version': <module 'matplotlib._version' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\_version.pyc'>, '_ast': <module '_ast' (built-in)>, '_pydev_bundle._pydev_filesystem_encoding': <module '_pydev_bundle._pydev_filesystem_encoding' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\_pydev_filesystem_encoding.py'>, 'pkg_resources.os': None, '_pydev_bundle._pydev_imports_tipper': <module '_pydev_bundle._pydev_imports_tipper' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\_pydev_imports_tipper.py'>, '_bisect': <module '_bisect' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'D:\ana\envs\py27\lib\encodings\aliases.pyc'>, 'fnmatch': <module 'fnmatch' from 'D:\ana\envs\py27\lib\fnmatch.pyc'>, 'sre_parse': <module 'sre_parse' from 'D:\ana\envs\py27\lib\sre_parse.pyc'>, 'pickle': <module 'pickle' from 'D:\ana\envs\py27\lib\pickle.pyc'>, 'numpy.random.warnings': None, 'numpy.lib.polynomial': <module 'numpy.lib.polynomial' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\polynomial.pyc'>, 'numpy.compat': <module 'numpy.compat' from 'D:\ana\envs\py27\lib\site-packages\numpy\compat\__init__.pyc'>, 'pkg_resources._vendor.os': None, 'pkg_resources.extern.six': <module 'pkg_resources._vendor.six' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\six.pyc'>, 'numbers': <module 'numbers' from 'D:\ana\envs\py27\lib\numbers.pyc'>, '_pydev_bundle.pydev_code_executor': <module '_pydev_bundle.pydev_code_executor' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_code_executor.py'>, 'numpy.core.records': <module 'numpy.core.records' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\records.pyc'>, 'pydevd_tracing': <module 'pydevd_tracing' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_tracing.py'>, '_pydevd_bundle.StringIO': None, 'email.Header': <email.LazyImporter object at 0x00000000134E9A48>, 'shutil': <module 'shutil' from 'D:\ana\envs\py27\lib\shutil.pyc'>, 'strop': <module 'strop' (built-in)>, 'numpy.core.numeric': <module 'numpy.core.numeric' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\numeric.pyc'>, 'six': <module 'six' from 'D:\ana\envs\py27\lib\site-packages\six.pyc'>, 'pydev_ipython.matplotlibtools': <module 'pydev_ipython.matplotlibtools' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydev_ipython\matplotlibtools.py'>, 'email.message': <module 'email.message' from 'D:\ana\envs\py27\lib\email\message.pyc'>, '_pydevd_bundle.imp': None, '_pydev_bundle.traceback': None, '_pydevd_bundle.json': None, 'numpy.lib.utils': <module 'numpy.lib.utils' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\utils.pyc'>, 'email.Charset': <email.LazyImporter object at 0x00000000134E9408>, 'encodings.utf_8': <module 'encodings.utf_8' from 'D:\ana\envs\py27\lib\encodings\utf_8.pyc'>, '_pydevd_frame_eval._pydev_bundle': None, 'codeop': <module 'codeop' from 'D:\ana\envs\py27\lib\codeop.pyc'>, 'numpy.lib.arrayterator': <module 'numpy.lib.arrayterator' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\arrayterator.pyc'>, 'email.parser': <module 'email.parser' from 'D:\ana\envs\py27\lib\email\parser.pyc'>, 'pkg_resources._vendor.__builtin__': None, 'os.path': <module 'ntpath' from 'D:\ana\envs\py27\lib\ntpath.pyc'>, 'pkg_resources.extern.packaging.specifiers': <module 'pkg_resources._vendor.packaging.specifiers' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\specifiers.pyc'>, 'email.base64mime': <module 'email.base64mime' from 'D:\ana\envs\py27\lib\email\base64mime.pyc'>, '_weakrefset': <module '_weakrefset' from 'D:\ana\envs\py27\lib\_weakrefset.pyc'>, 'pydevd_plugins.traceback': None, 'email.string': None, 'pydevd_concurrency_analyser': <module 'pydevd_concurrency_analyser' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_concurrency_analyser\__init__.py'>, 'unittest.traceback': None, 'unittest.os': None, '_pydevd_bundle.pydevd_cython_win32_27_64': <module '_pydevd_bundle.pydevd_cython_win32_27_64' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_cython_win32_27_64.pyd'>, '_pydevd_frame_eval.pydevd_frame_eval_main': <module '_pydevd_frame_eval.pydevd_frame_eval_main' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_frame_eval\pydevd_frame_eval_main.py'>, 'functools': <module 'functools' from 'D:\ana\envs\py27\lib\functools.pyc'>, 'pkg_resources._vendor.weakref': None, 'sysconfig': <module 'sysconfig' from 'D:\ana\envs\py27\lib\sysconfig.pyc'>, '_pydevd_bundle.pydevd_comm': <module '_pydevd_bundle.pydevd_comm' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py'>, 'email.MIMEImage': <email.LazyImporter object at 0x00000000134E9FC8>, '_pydevd_bundle.math': None, 'numpy.core.numerictypes': <module 'numpy.core.numerictypes' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\numerictypes.pyc'>, 'numpy.polynomial.legendre': <module 'numpy.polynomial.legendre' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\legendre.pyc'>, 'numpy.matrixlib.defmatrix': <module 'numpy.matrixlib.defmatrix' from 'D:\ana\envs\py27\lib\site-packages\numpy\matrixlib\defmatrix.pyc'>, 'tempfile': <module 'tempfile' from 'D:\ana\envs\py27\lib\tempfile.pyc'>, 'imp': <module 'imp' (built-in)>, 'pkg_resources._vendor': <module 'pkg_resources._vendor' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\__init__.pyc'>, 'matplotlib.compat': <module 'matplotlib.compat' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\compat\__init__.pyc'>, '_pydevd_bundle.pydevd_io': <module '_pydevd_bundle.pydevd_io' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_io.py'>, 'numpy.core': <module 'numpy.core' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\__init__.pyc'>, 'numpy.linalg.info': <module 'numpy.linalg.info' from 'D:\ana\envs\py27\lib\site-packages\numpy\linalg\info.pyc'>, '_pydevd_bundle.pydevd_process_net_command': <module '_pydevd_bundle.pydevd_process_net_command' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_process_net_command.py'>, 'unittest.functools': None, 'pkg_resources._vendor.six': <module 'pkg_resources._vendor.six' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\six.pyc'>, 'unittest.util': <module 'unittest.util' from 'D:\ana\envs\py27\lib\unittest\util.pyc'>, 'pkg_resources.errno': None, 'email.urllib': None, 'numpy.core.os': None, '_pydevd_bundle.pydevd_utils': <module '_pydevd_bundle.pydevd_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_utils.py'>, 'httplib': <module 'httplib' from 'D:\ana\envs\py27\lib\httplib.pyc'>, '_pydevd_bundle.pydevd_additional_thread_info_regular': <module '_pydevd_bundle.pydevd_additional_thread_info_regular' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_additional_thread_info_regular.py'>, 'decimal': <module 'decimal' from 'D:\ana\envs\py27\lib\decimal.pyc'>, '_pydevd_bundle.pydevd_console': <module '_pydevd_bundle.pydevd_console' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_console.py'>, 'numpy.lib._datasource': <module 'numpy.lib._datasource' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\_datasource.pyc'>, 'token': <module 'token' from 'D:\ana\envs\py27\lib\token.pyc'>, 'email.encoders': <module 'email.encoders' from 'D:\ana\envs\py27\lib\email\encoders.pyc'>, '_pydevd_bundle.pydevd_cython': <module '_pydevd_bundle.pydevd_cython_win32_27_64' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_cython_win32_27_64.pyd'>, '_pydev_bundle.urllib': None, 'numpy.linalg._umath_linalg': <module 'numpy.linalg._umath_linalg' from 'D:\ana\envs\py27\lib\site-packages\numpy\linalg\_umath_linalg.pyd'>, 'cStringIO': <module 'cStringIO' (built-in)>, 'numpy.polynomial': <module 'numpy.polynomial' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\__init__.pyc'>, 'numpy.core._add_newdocs': <module 'numpy.core._add_newdocs' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_add_newdocs.pyc'>, 'pkg_resources.extern.packaging.requirements': <module 'pkg_resources._vendor.packaging.requirements' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\requirements.pyc'>, '_pydevd_bundle.pydevd_frame_utils': <module '_pydevd_bundle.pydevd_frame_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_frame_utils.py'>, 'encodings': <module 'encodings' from 'D:\ana\envs\py27\lib\encodings\__init__.pyc'>, 'BaseHTTPServer': <module 'BaseHTTPServer' from 'D:\ana\envs\py27\lib\BaseHTTPServer.pyc'>, '_pydev_bundle._pydev_bundle': None, '_pydevd_bundle.inspect': None, '_pydev_bundle.pydev_override': <module '_pydev_bundle.pydev_override' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_override.py'>, 'threading': <module 'threading' from 'D:\ana\envs\py27\lib\threading.pyc'>, 'distutils.string': None, '_subprocess': <module '_subprocess' (built-in)>, 'numpy.random.threading': None, 're': <module 're' from 'D:\ana\envs\py27\lib\re.pyc'>, '_pydev_bundle.os': None, 'numpy.core.collections': None, 'email.quopri': None, 'pydevd_plugins.extensions.types.pydevd_plugin_numpy_types': <module 'pydevd_plugins.extensions.types.pydevd_plugin_numpy_types' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\extensions\types\pydevd_plugin_numpy_types.py'>, 'matplotlib.cbook': <module 'matplotlib.cbook' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\cbook\__init__.pyc'>, 'ast': <module 'ast' from 'D:\ana\envs\py27\lib\ast.pyc'>, 'numpy.lib.ufunclike': <module 'numpy.lib.ufunclike' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\ufunclike.pyc'>, '_pydev_imps.socket': None, 'ctypes.struct': None, 'numpy.core.sys': None, 'pydev_ipython.sys': None, '_pydevd_bundle.pydevd_xml': <module '_pydevd_bundle.pydevd_xml' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_xml.py'>, 'numpy.testing._private': <module 'numpy.testing._private' from 'D:\ana\envs\py27\lib\site-packages\numpy\testing\_private\__init__.pyc'>, 'matplotlib.json': None, '_locale': <module '_locale' (built-in)>, 'thread': <module 'thread' (built-in)>, 'traceback': <module 'traceback' from 'D:\ana\envs\py27\lib\traceback.pyc'>, '_pydev_bundle.codeop': None, '_pydevd_bundle.weakref': None, 'pkg_resources._vendor.packaging.markers': <module 'pkg_resources._vendor.packaging.markers' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\markers.pyc'>, 'pkg_resources._vendor.packaging.version': <module 'pkg_resources._vendor.packaging.version' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\version.pyc'>, 'pydevd_plugins.extensions.types.pydevd_helpers': <module 'pydevd_plugins.extensions.types.pydevd_helpers' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\extensions\types\pydevd_helpers.py'>, '_pydev_imps.select': None, '_pydevd_bundle.urllib': None, '_collections': <module '_collections' (built-in)>, 'numpy.random': <module 'numpy.random' from 'D:\ana\envs\py27\lib\site-packages\numpy\random\__init__.pyc'>, '_pydev_imps.BaseHTTPServer': None, 'numpy.lib.twodim_base': <module 'numpy.lib.twodim_base' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\twodim_base.pyc'>, 'pydevd_plugins.inspect': None, 'xml.parsers.pyexpat': None, 'array': <module 'array' (built-in)>, 'email.Errors': <email.LazyImporter object at 0x00000000134E9108>, 'pydevd': <module 'pydevd' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd.py'>, 'ctypes.sys': None, 'posixpath': <module 'posixpath' from 'D:\ana\envs\py27\lib\posixpath.pyc'>, '_pydev_bundle.re': None, 'pkg_resources._vendor.sys': None, 'pkg_resources._vendor.packaging._compat': <module 'pkg_resources._vendor.packaging._compat' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\_compat.pyc'>, 'numpy.core.arrayprint': <module 'numpy.core.arrayprint' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\arrayprint.pyc'>, '_pydev_bundle.pydev_is_thread_alive': <module '_pydev_bundle.pydev_is_thread_alive' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_is_thread_alive.py'>, 'types': <module 'types' from 'D:\ana\envs\py27\lib\types.pyc'>, 'numpy.lib.stride_tricks': <module 'numpy.lib.stride_tricks' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\stride_tricks.pyc'>, 'numpy.lib.scimath': <module 'numpy.lib.scimath' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\scimath.pyc'>, 'matplotlib.cbook.functools': None, 'json._json': None, '_pydevd_bundle.dis': None, '_codecs': <module '_codecs' (built-in)>, 'numpy.__config__': <module 'numpy.__config__' from 'D:\ana\envs\py27\lib\site-packages\numpy\__config__.pyc'>, 'numpy.core._string_helpers': <module 'numpy.core._string_helpers' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_string_helpers.pyc'>, 'copy': <module 'copy' from 'D:\ana\envs\py27\lib\copy.pyc'>, '_pydev_bundle._pydev_calltip_util': <module '_pydev_bundle._pydev_calltip_util' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\_pydev_calltip_util.py'>, 'hashlib': <module 'hashlib' from 'D:\ana\envs\py27\lib\hashlib.pyc'>, 'keyword': <module 'keyword' from 'D:\ana\envs\py27\lib\keyword.pyc'>, 'pkg_resources._vendor.pyparsing': <module 'pkg_resources._vendor.pyparsing' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\pyparsing.pyc'>, 'numpy.lib.nanfunctions': <module 'numpy.lib.nanfunctions' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\nanfunctions.pyc'>, 'unittest.weakref': None, 'pkg_resources.sys': None, '_pydevd_bundle.pickle': None, '_pydevd_bundle.pydevd_custom_frames': <module '_pydevd_bundle.pydevd_custom_frames' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_custom_frames.py'>, 'sre_compile': <module 'sre_compile' from 'D:\ana\envs\py27\lib\sre_compile.pyc'>, 'pkg_resources._vendor.threading': None, '_hashlib': <module '_hashlib' from 'D:\ana\envs\py27\DLLs\_hashlib.pyd'>, 'numpy.lib.shape_base': <module 'numpy.lib.shape_base' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\shape_base.pyc'>, 'getpass': <module 'getpass' from 'D:\ana\envs\py27\lib\getpass.pyc'>, 'email.base64': None, '_pydevd_bundle.platform': None, '__main__': <module '__main__' from 'D:/test/code/all_single/badcase.py'>, 'numpy.fft.info': <module 'numpy.fft.info' from 'D:\ana\envs\py27\lib\site-packages\numpy\fft\info.pyc'>, 'numpy.sys': None, 'numpy.core._dtype': <module 'numpy.core._dtype' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_dtype.pyc'>, 'dateutil._version': <module 'dateutil._version' from 'D:\ana\envs\py27\lib\site-packages\dateutil\_version.pyc'>, 'unittest.result': <module 'unittest.result' from 'D:\ana\envs\py27\lib\unittest\result.pyc'>, 'bz2': <module 'bz2' from 'D:\ana\envs\py27\DLLs\bz2.pyd'>, 'encodings.codecs': None, 'pydevd_plugins.extensions.types': <module 'pydevd_plugins.extensions.types' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\extensions\types\__init__.py'>, '_pydevd_bundle.cStringIO': None, 'email.MIMEMultipart': <email.LazyImporter object at 0x00000000134E7148>, 'unittest.difflib': None, '_ssl': <module '_ssl' from 'D:\ana\envs\py27\DLLs\_ssl.pyd'>, 'numpy.lib.index_tricks': <module 'numpy.lib.index_tricks' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\index_tricks.pyc'>, 'trace': <module 'trace' from 'D:\ana\envs\py27\lib\trace.pyc'>, 'warnings': <module 'warnings' from 'D:\ana\envs\py27\lib\warnings.pyc'>, 'glob': <module 'glob' from 'D:\ana\envs\py27\lib\glob.pyc'>, '_pydevd_frame_eval.sys': None, '_pydevd_bundle.pydevd_command_line_handling': <module '_pydevd_bundle.pydevd_command_line_handling' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_command_line_handling.py'>, 'pkg_resources.extern.appdirs': <module 'pkg_resources._vendor.appdirs' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\appdirs.pyc'>, '_pydev_bundle.xmlrpclib': None, 'json.sys': None, 'email.MIMEBase': <email.LazyImporter object at 0x00000000134E9F88>, 'future_builtins': <module 'future_builtins' (built-in)>, '_io': <module '_io' (built-in)>, 'linecache': <module 'linecache' from 'D:\ana\envs\py27\lib\linecache.pyc'>, 'numpy.linalg.linalg': <module 'numpy.linalg.linalg' from 'D:\ana\envs\py27\lib\site-packages\numpy\linalg\linalg.pyc'>, '_pydev_bundle.pydev_monkey_qt': <module '_pydev_bundle.pydev_monkey_qt' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_monkey_qt.py'>, 'numpy.lib._iotools': <module 'numpy.lib._iotools' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\_iotools.pyc'>, 'email.iterators': <module 'email.iterators' from 'D:\ana\envs\py27\lib\email\iterators.pyc'>, 'random': <module 'random' from 'D:\ana\envs\py27\lib\random.pyc'>, 'unittest.types': None, 'datetime': <module 'datetime' (built-in)>, 'ctypes._endian': <module 'ctypes._endian' from 'D:\ana\envs\py27\lib\ctypes\_endian.pyc'>, 'encodings.encodings': None, 'pyparsing': <module 'pyparsing' from 'D:\ana\envs\py27\lib\site-packages\pyparsing.pyc'>, 'numpy.random.mtrand': <module 'numpy.random.mtrand' from 'D:\ana\envs\py27\lib\site-packages\numpy\random\mtrand.pyd'>, 'xml': <module 'xml' from 'D:\ana\envs\py27\lib\xml\__init__.pyc'>, 'email.time': None, '_pydev_bundle._pydev_tipper_common': <module '_pydev_bundle._pydev_tipper_common' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\_pydev_tipper_common.py'>, 'importlib': <module 'importlib' from 'D:\ana\envs\py27\lib\importlib\__init__.pyc'>, 'six.moves.urllib.request': <module 'six.moves.urllib.request' (built-in)>, 'numpy.core._dtype_ctypes': <module 'numpy.core._dtype_ctypes' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_dtype_ctypes.pyc'>, 'pyexpat.errors': <module 'pyexpat.errors' (built-in)>, 'pydevd_file_utils': <module 'pydevd_file_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_file_utils.py'>, 'pydevd_plugins.extensions': <module 'pydevd_plugins.extensions' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\extensions\__init__.py'>, 'numpy.core._type_aliases': <module 'numpy.core._type_aliases' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_type_aliases.pyc'>, 'numpy.lib._version': <module 'numpy.lib._version' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\_version.pyc'>, '_pydevd_frame_eval': <module '_pydevd_frame_eval' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_frame_eval\__init__.py'>, 'zipfile': <module 'zipfile' from 'D:\ana\envs\py27\lib\zipfile.pyc'>, 'ssl': <module 'ssl' from 'D:\ana\envs\py27\lib\ssl.pyc'>, 'numpy.version': <module 'numpy.version' from 'D:\ana\envs\py27\lib\site-packages\numpy\version.pyc'>, 'distutils.re': None, 'numpy.lib.type_check': <module 'numpy.lib.type_check' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\type_check.pyc'>, 'bisect': <module 'bisect' from 'D:\ana\envs\py27\lib\bisect.pyc'>, 'unittest.re': None, '_pydev_imps.sys': None, 'pyexpat.model': <module 'pyexpat.model' (built-in)>, 'pkg_resources._vendor.six.moves': <module 'pkg_resources._vendor.six.moves' (built-in)>, 'cycler': <module 'cycler' from 'D:\ana\envs\py27\lib\site-packages\cycler.pyc'>, 'email.Message': <email.LazyImporter object at 0x00000000134E9BC8>, '_pydev_bundle.thread': None, 'locale': <module 'locale' from 'D:\ana\envs\py27\lib\locale.pyc'>, 'atexit': <module 'atexit' from 'D:\ana\envs\py27\lib\atexit.pyc'>, 'email.quopriMIME': <email.LazyImporter object at 0x00000000134E9E08>, 'pkg_resources._vendor.re': None, 'calendar': <module 'calendar' from 'D:\ana\envs\py27\lib\calendar.pyc'>, 'matplotlib._color_data': <module 'matplotlib._color_data' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\_color_data.pyc'>, 'dateutil': <module 'dateutil' from 'D:\ana\envs\py27\lib\site-packages\dateutil\__init__.pyc'>, '_pydevd_bundle.pydevd_save_locals': <module '_pydevd_bundle.pydevd_save_locals' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_save_locals.py'>, 'plistlib': <module 'plistlib' from 'D:\ana\envs\py27\lib\plistlib.pyc'>, '_pydev_bundle.sys': None, '_pydevd_bundle.ctypes': None, 'urllib': <module 'urllib' from 'D:\ana\envs\py27\lib\urllib.pyc'>, 'email.MIMEText': <email.LazyImporter object at 0x00000000134E72C8>, 'pkg_resources.extern.packaging.version': <module 'pkg_resources._vendor.packaging.version' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\version.pyc'>, 'ctypes.wintypes': <module 'ctypes.wintypes' from 'D:\ana\envs\py27\lib\ctypes\wintypes.pyc'>, 'pkg_resources._vendor.string': None, 'email': <module 'email' from 'D:\ana\envs\py27\lib\email\__init__.pyc'>, '_pydevd_bundle.pydevd_vm_type': <module '_pydevd_bundle.pydevd_vm_type' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_vm_type.py'>, '_pydevd_bundle._pydevd_bundle': None, 'unittest.case': <module 'unittest.case' from 'D:\ana\envs\py27\lib\unittest\case.pyc'>, 'distutils.os': None, 'Queue': <module 'Queue' from 'D:\ana\envs\py27\lib\Queue.pyc'>, 'numpy.lib.info': <module 'numpy.lib.info' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\info.pyc'>, 'ctypes': <module 'ctypes' from 'D:\ana\envs\py27\lib\ctypes\__init__.pyc'>, '_ctypes': <module '_ctypes' from 'D:\ana\envs\py27\DLLs\_ctypes.pyd'>, '_pydevd_bundle.pydevd_breakpointhook': <module '_pydevd_bundle.pydevd_breakpointhook' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_breakpointhook.py'>, 'json.re': None, 'unittest.signal': None, 'itertools': <module 'itertools' (built-in)>, 'numpy.fft.fftpack': <module 'numpy.fft.fftpack' from 'D:\ana\envs\py27\lib\site-packages\numpy\fft\fftpack.pyc'>, 'opcode': <module 'opcode' from 'D:\ana\envs\py27\lib\opcode.pyc'>, '_pydevd_bundle.pydevd_console_integration': <module '_pydevd_bundle.pydevd_console_integration' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_console_integration.py'>, 'six.moves': <module 'six.moves' (built-in)>, 'numpy.testing._private.nosetester': <module 'numpy.testing._private.nosetester' from 'D:\ana\envs\py27\lib\site-packages\numpy\testing\_private\nosetester.pyc'>, '_pydevd_bundle.pydevd_resolver': <module '_pydevd_bundle.pydevd_resolver' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_resolver.py'>, 'email.uu': None, 'pydevd_plugins.extensions.types.sys': None, 'unittest': <module 'unittest' from 'D:\ana\envs\py27\lib\unittest\__init__.pyc'>, 'numpy.core._methods': <module 'numpy.core._methods' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_methods.pyc'>, '_pydevd_bundle.pydevd_trace_dispatch': <module '_pydevd_bundle.pydevd_trace_dispatch' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_trace_dispatch.py'>, 'xml.parsers': <module 'xml.parsers' from 'D:\ana\envs\py27\lib\xml\parsers\__init__.pyc'>, 'platform': <module 'platform' from 'D:\ana\envs\py27\lib\platform.pyc'>, 'numpy.testing._private.utils': <module 'numpy.testing._private.utils' from 'D:\ana\envs\py27\lib\site-packages\numpy\testing\_private\utils.pyc'>, 'unittest.collections': None, 'pkgutil': <module 'pkgutil' from 'D:\ana\envs\py27\lib\pkgutil.pyc'>, 'numpy.polynomial.laguerre': <module 'numpy.polynomial.laguerre' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\laguerre.pyc'>, 'pydevd_plugins._pydevd_bundle': None, 'sre_constants': <module 'sre_constants' from 'D:\ana\envs\py27\lib\sre_constants.pyc'>, 'json': <module 'json' from 'D:\ana\envs\py27\lib\json\__init__.pyc'>, 'numpy.core.function_base': <module 'numpy.core.function_base' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\function_base.pyc'>, '_random': <module '_random' (built-in)>, 'pkg_resources._vendor.sre_constants': None, 'numpy': <module 'numpy' from 'D:\ana\envs\py27\lib\site-packages\numpy\__init__.pyc'>, '_pydev_bundle.SimpleXMLRPCServer': None, 'SimpleXMLRPCServer': <module 'SimpleXMLRPCServer' from 'D:\ana\envs\py27\lib\SimpleXMLRPCServer.pyc'>, 'email.utils': <module 'email.utils' from 'D:\ana\envs\py27\lib\email\utils.pyc'>, 'numpy.ma': <module 'numpy.ma' from 'D:\ana\envs\py27\lib\site-packages\numpy\ma\__init__.pyc'>, 'nturl2path': <module 'nturl2path' from 'D:\ana\envs\py27\lib\nturl2path.pyc'>, 'matplotlib.cbook._backports': <module 'matplotlib.cbook._backports' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\cbook\_backports.pyc'>, 'email.MIMENonMultipart': <email.LazyImporter object at 0x00000000134E7248>, 'email.email': None, 'pydevd_plugins.extensions.types.collections': None, 'numpy.lib': <module 'numpy.lib' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\__init__.pyc'>, '_pydevd_bundle.pydevd_plugins': None, '_pydev_bundle.pydev_console_types': <module '_pydev_bundle.pydev_console_types' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_console_types.py'>, '_pydevd_bundle.trace': None, 'email.random': None, '_pydev_imps.SimpleXMLRPCServer': None, 'pydevd_concurrency_analyser.urllib': None, 'pydevd_concurrency_analyser.traceback': None, 'json.decoder': <module 'json.decoder' from 'D:\ana\envs\py27\lib\json\decoder.pyc'>, 'distutils.distutils': None, 'copy_reg': <module 'copy_reg' from 'D:\ana\envs\py27\lib\copy_reg.pyc'>, '_pydevd_bundle.warnings': None, '_pydevd_bundle.pydevd_collect_try_except_info': <module '_pydevd_bundle.pydevd_collect_try_except_info' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_collect_try_except_info.py'>, 'pkg_resources._vendor.appdirs': <module 'pkg_resources._vendor.appdirs' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\appdirs.pyc'>, 'pydevd_plugins._pydev_bundle': None, '_pydevd_bundle.pydevd_trace_api': <module '_pydevd_bundle.pydevd_trace_api' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_trace_api.py'>, 'site': <module 'site' from 'D:\ana\envs\py27\lib\site.pyc'>, 'io': <module 'io' from 'D:\ana\envs\py27\lib\io.pyc'>, 'pyexpat': <module 'pyexpat' from 'D:\ana\envs\py27\DLLs\pyexpat.pyd'>, '_pydevd_bundle.collections': None, '_pydev_bundle.inspect': None, 'email.binascii': None, 'unittest.time': None, 'matplotlib.rcsetup': <module 'matplotlib.rcsetup' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\rcsetup.pyc'>, 'encodings._multibytecodec': None, '_pydevd_bundle.socket': None, 'pkg_resources.extern': <module 'pkg_resources.extern' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\extern\__init__.pyc'>, 'rfc822': <module 'rfc822' from 'D:\ana\envs\py27\lib\rfc822.pyc'>, 'numpy.polynomial.polyutils': <module 'numpy.polynomial.polyutils' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\polyutils.pyc'>, 'json.json': None, 'email.re': None, 'sys': <module 'sys' (built-in)>, 'numpy.compat._inspect': <module 'numpy.compat._inspect' from 'D:\ana\envs\py27\lib\site-packages\numpy\compat\_inspect.pyc'>, '_pydev_bundle.pydev_stdin': <module '_pydev_bundle.pydev_stdin' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_stdin.py'>, 'email.cStringIO': None, 'pydevd_plugins.extensions.types._pydevd_bundle': None, 'pkg_resources._vendor.packaging': <module 'pkg_resources._vendor.packaging' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\__init__.pyc'>, 'matplotlib.fontconfig_pattern': <module 'matplotlib.fontconfig_pattern' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\fontconfig_pattern.pyc'>, '_weakref': <module '_weakref' (built-in)>, 'difflib': <module 'difflib' from 'D:\ana\envs\py27\lib\difflib.pyc'>, 'distutils.errors': <module 'distutils.errors' from 'D:\ana\envs\py27\lib\distutils\errors.pyc'>, 'pkg_resources.extern.pyparsing': <module 'pkg_resources._vendor.pyparsing' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\pyparsing.pyc'>, 'urlparse': <module 'urlparse' from 'D:\ana\envs\py27\lib\urlparse.pyc'>, '_pydev_bundle.__future__': None, 'unittest.warnings': None, 'gzip': <module 'gzip' from 'D:\ana\envs\py27\lib\gzip.pyc'>, '_pydevd_bundle.pydevd_kill_all_pydevd_threads': <module '_pydevd_bundle.pydevd_kill_all_pydevd_threads' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_kill_all_pydevd_threads.py'>, 'heapq': <module 'heapq' from 'D:\ana\envs\py27\lib\heapq.pyc'>, 'numpy.core.functools': None, 'distutils': <module 'distutils' from 'D:\ana\envs\py27\lib\distutils\__init__.pyc'>, '_pydev_bundle.StringIO': None, 'numpy.core.einsumfunc': <module 'numpy.core.einsumfunc' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\einsumfunc.pyc'>, '_pydevd_bundle.struct': None, 'pkg_resources.extern.packaging': <module 'pkg_resources._vendor.packaging' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\__init__.pyc'>, 'matplotlib.cbook.deprecation': <module 'matplotlib.cbook.deprecation' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\cbook\deprecation.pyc'>, 'matplotlib.colors': <module 'matplotlib.colors' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\colors.pyc'>, 'email.mime': <module 'email.mime' from 'D:\ana\envs\py27\lib\email\mime\__init__.pyc'>, 'msvcrt': <module 'msvcrt' (built-in)>, '_pydevd_bundle.pydevd_breakpoints': <module '_pydevd_bundle.pydevd_breakpoints' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_breakpoints.py'>, 'struct': <module 'struct' from 'D:\ana\envs\py27\lib\struct.pyc'>, 'numpy.core.ctypes': None, 'pydevd_plugins.extensions.types.pydevd_plugins_django_form_str': <module 'pydevd_plugins.extensions.types.pydevd_plugins_django_form_str' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\extensions\types\pydevd_plugins_django_form_str.py'>, 'numpy.testing': <module 'numpy.testing' from 'D:\ana\envs\py27\lib\site-packages\numpy\testing\__init__.pyc'>, '_pydevd_bundle.pydevd_file_utils': None, 'collections': <module 'collections' from 'D:\ana\envs\py27\lib\collections.pyc'>, '_pydevd_bundle.pydevd_exec': <module '_pydevd_bundle.pydevd_exec' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_exec.py'>, '_sre': <module '_sre' (built-in)>, 'numpy.core._multiarray_umath': <module 'numpy.core._multiarray_umath' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\_multiarray_umath.pyd'>, 'unittest.main': <module 'unittest.main' from 'D:\ana\envs\py27\lib\unittest\main.pyc'>, 'email.Parser': <email.LazyImporter object at 0x00000000134E9C88>, '_pydev_bundle.types': None, 'distutils.types': None, 'zipimport': <module 'zipimport' (built-in)>, 'pkg_resources._vendor.packaging._structures': <module 'pkg_resources._vendor.packaging._structures' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\_structures.pyc'>, 'pkg_resources.extern.sys': None, 'pkg_resources.extern.packaging.markers': <module 'pkg_resources._vendor.packaging.markers' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\markers.pyc'>, 'textwrap': <module 'textwrap' from 'D:\ana\envs\py27\lib\textwrap.pyc'>, '_pydevd_bundle.__future__': None, '_pydevd_bundle.pydevd_console_pytest': <module '_pydevd_bundle.pydevd_console_pytest' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_console_pytest.py'>, '_pydev_imps.Queue': None, '_pydev_imps.time': None, '_pydevd_bundle.traceback': None, '_pydev_bundle._pydev_imps': None, 'pydevd_concurrency_analyser._pydev_imps': None, 'signal': <module 'signal' (built-in)>, 'numpy.random.operator': None, 'email.calendar': None, '_pydevd_bundle.pydevd_additional_thread_info': <module '_pydevd_bundle.pydevd_additional_thread_info' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_additional_thread_info.py'>, '_pydevd_bundle.opcode': None, 'quopri': <module 'quopri' from 'D:\ana\envs\py27\lib\quopri.pyc'>, '_pydev_bundle.Queue': None, 'distutils.version': <module 'distutils.version' from 'D:\ana\envs\py27\lib\distutils\version.pyc'>, 'pkg_resources.py31compat': <module 'pkg_resources.py31compat' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\py31compat.pyc'>, 'google': <module 'google' (built-in)>, 'pydevd_concurrency_analyser.pydevd_file_utils': None, '_pydevd_bundle.pkgutil': None, 'numpy.core.getlimits': <module 'numpy.core.getlimits' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\getlimits.pyc'>, 'xml.parsers.expat': <module 'xml.parsers.expat' from 'D:\ana\envs\py27\lib\xml\parsers\expat.pyc'>, '_pydevd_bundle.pydevd_frame': <module '_pydevd_bundle.pydevd_frame' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_frame.py'>, 'matplotlib.compat.subprocess': <module 'matplotlib.compat.subprocess' from 'D:\ana\envs\py27\lib\site-packages\matplotlib\compat\subprocess.pyc'>, '_pydevd_bundle.re': None, 'pydev_ipython.select': None, 'pydevd_concurrency_analyser._pydev_bundle': None, 'email.Generator': <email.LazyImporter object at 0x00000000134E99C8>, 'pydevd_plugins.pydevd_file_utils': None, 'numpy.matrixlib': <module 'numpy.matrixlib' from 'D:\ana\envs\py27\lib\site-packages\numpy\matrixlib\__init__.pyc'>, '_pydevd_bundle.pydevd_tracing': None, 'email.MIMEAudio': <email.LazyImporter object at 0x00000000134E9F08>, 'pydevd_plugins.jinja2_debug': <module 'pydevd_plugins.jinja2_debug' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\jinja2_debug.py'>, 'backports': <module 'backports' from 'D:\ana\envs\py27\lib\site-packages\backports\__init__.pyc'>, '_pydev_bundle._pydevd_bundle': None, 'numpy.lib.mixins': <module 'numpy.lib.mixins' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\mixins.pyc'>, 'email.errors': <module 'email.errors' from 'D:\ana\envs\py27\lib\email\errors.pyc'>, '_pydevd_bundle.__builtin__': None, 'mpl_toolkits': <module 'mpl_toolkits' (built-in)>, 'UserDict': <module 'UserDict' from 'D:\ana\envs\py27\lib\UserDict.pyc'>, 'inspect': <module 'inspect' from 'D:\ana\envs\py27\lib\inspect.pyc'>, 'numpy.linalg': <module 'numpy.linalg' from 'D:\ana\envs\py27\lib\site-packages\numpy\linalg\__init__.pyc'>, 'numpy.core.multiarray': <module 'numpy.core.multiarray' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\multiarray.pyc'>, 'unittest.runner': <module 'unittest.runner' from 'D:\ana\envs\py27\lib\unittest\runner.pyc'>, '_pydev_bundle.warnings': None, 'unittest.loader': <module 'unittest.loader' from 'D:\ana\envs\py27\lib\unittest\loader.pyc'>, '_functools': <module '_functools' (built-in)>, 'email.Iterators': <email.LazyImporter object at 0x00000000134E9B88>, 'socket': <module 'socket' from 'D:\ana\envs\py27\lib\socket.pyc'>, 'numpy.core.memmap': <module 'numpy.core.memmap' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\memmap.pyc'>, 'numpy.core.overrides': <module 'numpy.core.overrides' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\overrides.pyc'>, 'email.base64MIME': <email.LazyImporter object at 0x00000000134E9D48>, 'cython_runtime': <module 'cython_runtime' (built-in)>, '_pydevd_bundle.sys': None, 'numpy.linalg.lapack_lite': <module 'numpy.linalg.lapack_lite' from 'D:\ana\envs\py27\lib\site-packages\numpy\linalg\lapack_lite.pyd'>, 'os': <module 'os' from 'D:\ana\envs\py27\lib\os.pyc'>, 'marshal': <module 'marshal' (built-in)>, '__future__': <module '__future__' from 'D:\ana\envs\py27\lib\__future__.pyc'>, 'numpy.core.shape_base': <module 'numpy.core.shape_base' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\shape_base.pyc'>, '_pydevd_bundle.pydevd_constants': <module '_pydevd_bundle.pydevd_constants' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_constants.py'>, '_multibytecodec': <module '_multibytecodec' (built-in)>, '_pydev_bundle': <module '_pydev_bundle' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\__init__.py'>, '__builtin__': <module '__builtin__' (built-in)>, 'numpy.ma.core': <module 'numpy.ma.core' from 'D:\ana\envs\py27\lib\site-packages\numpy\ma\core.pyc'>, 'operator': <module 'operator' (built-in)>, 'json.struct': None, 'email.feedparser': <module 'email.feedparser' from 'D:\ana\envs\py27\lib\email\feedparser.pyc'>, '_pydevd_frame_eval._pydevd_bundle': None, '_pydevd_bundle.pydevd_bytecode_utils': <module '_pydevd_bundle.pydevd_bytecode_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_bytecode_utils.py'>, 'errno': <module 'errno' (built-in)>, 'pkg_resources._vendor.packaging.requirements': <module 'pkg_resources._vendor.packaging.requirements' from 'D:\ana\envs\py27\lib\site-packages\pkg_resources\_vendor\packaging\requirements.pyc'>, '_socket': <module '_socket' from 'D:\ana\envs\py27\DLLs\_socket.pyd'>, '_pydev_imps.xmlrpclib': None, 'email.Utils': <email.LazyImporter object at 0x00000000134E9CC8>, '_pydevd_bundle.pydevd_dont_trace': <module '_pydevd_bundle.pydevd_dont_trace' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_dont_trace.py'>, 'numpy.lib.histograms': <module 'numpy.lib.histograms' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\histograms.pyc'>, 'encodings.__builtin__': None, '_pydevd_bundle.itertools': None, 'pkg_resources._vendor.ctypes': None, 'unittest.fnmatch': None, 'email.os': None, 'numpy._distributor_init': <module 'numpy._distributor_init' from 'D:\ana\envs\py27\lib\site-packages\numpy\_distributor_init.pyc'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_pydev_bundle.pydev_imports': <module '_pydev_bundle.pydev_imports' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_imports.py'>, '_pydev_imps.threading': None, '_struct': <module '_struct' (built-in)>, 'pkg_resources._vendor.six.moves.urllib': <module 'pkg_resources._vendor.six.moves.urllib' (built-in)>, '_pydev_bundle.getpass': None, 'uu': <module 'uu' from 'D:\ana\envs\py27\lib\uu.pyc'>, 'numpy.fft': <module 'numpy.fft' from 'D:\ana\envs\py27\lib\site-packages\numpy\fft\__init__.pyc'>, 'numpy.random.numpy': None, '_pydevd_bundle.contextlib': None, 'numpy.lib.function_base': <module 'numpy.lib.function_base' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\function_base.pyc'>, '_pydev_bundle.fix_getpass': <module '_pydev_bundle.fix_getpass' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\fix_getpass.py'>, 'mimetools': <module 'mimetools' from 'D:\ana\envs\py27\lib\mimetools.pyc'>, '_pydevd_bundle.pydevd_import_class': <module '_pydevd_bundle.pydevd_import_class' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_import_class.py'>, '_pydevd_bundle._pydev_bundle': None, 'numpy.compat.py3k': <module 'numpy.compat.py3k' from 'D:\ana\envs\py27\lib\site-packages\numpy\compat\py3k.pyc'>, 'pydevd_concurrency_analyser._pydevd_bundle': None, '_cython_0_29_11': <module '_cython_0_29_11' (built-in)>, 'numpy.polynomial._polybase': <module 'numpy.polynomial._polybase' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\_polybase.pyc'>, '_pydev_bundle.__builtin__': None, 'numpy.polynomial.hermite': <module 'numpy.polynomial.hermite' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\hermite.pyc'>, 'contextlib': <module 'contextlib' from 'D:\ana\envs\py27\lib\contextlib.pyc'>, '_pydevd_bundle.pydevd_extension_utils': <module '_pydevd_bundle.pydevd_extension_utils' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_extension_utils.py'>, 'numpy.polynomial.polynomial': <module 'numpy.polynomial.polynomial' from 'D:\ana\envs\py27\lib\site-packages\numpy\polynomial\polynomial.pyc'>, 'pydevd_concurrency_analyser.Queue': None, '_pydev_imps._pydev_saved_modules': <module '_pydev_imps._pydev_saved_modules' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_saved_modules.py'>, 'pkg_resources.extern.six.moves': <module 'pkg_resources._vendor.six.moves' (built-in)>, 'importlib.sys': None, 'numpy.core.defchararray': <module 'numpy.core.defchararray' from 'D:\ana\envs\py27\lib\site-packages\numpy\core\defchararray.pyc'>, '_abcoll': <module '_abcoll' from 'D:\ana\envs\py27\lib\_abcoll.pyc'>, 'nt': <module 'nt' (built-in)>, 'genericpath': <module 'genericpath' from 'D:\ana\envs\py27\lib\genericpath.pyc'>, 'stat': <module 'stat' from 'D:\ana\envs\py27\lib\stat.pyc'>, 'urllib2': <module 'urllib2' from 'D:\ana\envs\py27\lib\urllib2.pyc'>, 'unittest.signals': <module 'unittest.signals' from 'D:\ana\envs\py27\lib\unittest\signals.pyc'>, 'pydevd_plugins.django_debug': <module 'pydevd_plugins.django_debug' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\django_debug.py'>, 'numpy.core.warnings': None, 'email.Encoders': <email.LazyImporter object at 0x00000000134E9208>, 'backports.functools_lru_cache': <module 'backports.functools_lru_cache' from 'D:\ana\envs\py27\lib\site-packages\backports\functools_lru_cache.pyc'>, 'ctypes.ctypes': None, 'numpy.lib.format': <module 'numpy.lib.format' from 'D:\ana\envs\py27\lib\site-packages\numpy\lib\format.pyc'>, 'sitecustomize': <module 'sitecustomize' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\sitecustomize.pyc'>, 'numpy.testing._private.decorators': <module 'numpy.testing._private.decorators' from 'D:\ana\envs\py27\lib\site-packages\numpy\testing\_private\decorators.pyc'>, 'xmlrpclib': <module 'xmlrpclib' from 'D:\ana\envs\py27\lib\xmlrpclib.pyc'>, 'encodings.mbcs': <module 'encodings.mbcs' from 'D:\ana\envs\py27\lib\encodings\mbcs.pyc'>, 'pkg_resources._vendor.types': None, '_pydev_bundle.pydev_monkey': <module '_pydev_bundle.pydev_monkey' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_monkey.py'>, '_pydev_imps.thread': None, 'time': <module 'time' (built-in)>, 'pydevd_plugins': <module 'pydevd_plugins' from 'D:\pycharm\PyCharm 2020.1\plugins\python\helpers\pydev\pydevd_plugins\__init__.py'>}
sys.platform
获取当前执行环境的平台,如win32表示是Windows系统,linux2表示是linux平台
sys.getrecursionlimit
python默认支持的递归数量
sys.stdout.write
可以做输出重定向
for i in range(3):
print("魔降风云变")
import sys
for i in range(3):
sys.stdout.write('小马过河')
-----------------结果:
魔降风云变
魔降风云变
魔降风云变
小马过河小马过河小马过河
import sys
for i in range(3):
sys.stderr.write('小马过河')
------------------结果:
小马过河小马过河小马过河
stdout 是一个类文件对象;调用它的 write 函数可以打印出你给定的任何字符串。
实际上,这就是 print 函数真正做的事情;它在你打印的字符串后面加上一个硬回车,然后调用 sys.stdout.write 函数。
在最简单的例子中,stdout 和 stderr 把它们的输出发送到相同的地方
和 stdout 一样,stderr 并不为你添加硬回车;如果需要,要自己加上。
stdout 和 stderr 都是类文件对象,但是它们都是只写的。
它们都没有 read 方法,只有 write 方法。然而,它们仍然是类文件对象,因此你可以将其它任何 (类) 文件对象赋值给它们来重定向其输出
标准流stdin&stdout&stderr
描述:
sys.stdin ---- 标准输入函数,类似于input()
sys.stdput----标准输出函数,类似于print()
sys.stderr----标准错误输出函数
实际上,input()和print()都是通过调用标准流实现,sys.stdin和sys.stdout实质上不过是两个interface接口,用于切换不同i/o
sys.stdin vs input()
- sys.stdin.readline() 用于读取一行文本输入,直到按回车,注意,使用该函数时,回车符也被读入
>>> import sys
>>> l=sys.stdin.readline()
123
>>>print(l)
# 输出 '123\n'
>>> len(l)
# 输出 4
- sys.stdin.readlines() 用于读取多行文本输入,按下 回车 后,无法退出输入,需要 再 按下 Ctrl+z 然后 再按 回车
>>>import sys
>>>> i=sys.stdin.readlines()
1
2
3
a
b
c
^Z
>>> print(i)
['1\n', '2\n', '3\n', 'a\n', 'b\n', 'c\n']
- input() 用于读取一行文本输入,回车符结束输入,但是不会被包含在输入内
总结:
sys.stdin的两种输入都会包含回车,而input()不会
sys.std和input() 两种输入的返回对象都是字符型,可以通过int()、floadt() 等 进行强制类型转换
-
sys.stdin消去换行符:
- 法一:sys.stdin.readline().strip(’/n’)
- 法二:sys.stdin.readline()[:-1]
sys.stdout vs print()
- print:python在调用print的过程中,实际上是引用了sys.stdout.write(obj+’/n’)
即 print()结束时默认换行,若想实现不自动换行,可使用print(param,end=’ ') - sys.stdout.write() 方法把字符写入到标准输出中,也就是控制台。
该方法默认不换行,若想实现换行,可使用sys.stdout.write(‘str/n’) - 区别:print()几乎可以打印所有类型的数据,但是sys.stdout.write()只接受字符型数据
eg
>>> a=1
>>> type(a)
<class 'int'>
>>> sys.stdout.write(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not int
>>> print(a)
1
重定向
1、重定向流到文件或程序
在Windows和类Unix平台下,可以利用shell语法 < filename
把标准输入流重定向到文件输入
eg:以下是一个示例脚本,命名为 test.py
def interact():
print('Hello stream world') #正常输出
while True:
try:
reply=input('Enter a number >') #输入(读取)来自sys.stdin的数据(也就是标准输入流的数据)
except EOFError: #当文件结束时,抛出异常,结束循环
break
else:
num=int(reply)
print("%d squared is %d" % (num,num**2))
print('Bye')
if __name__=='__main__':
interact()
>>> python test.py # 路径因人而异
Hello stream world
Enter a number >8 #输入8
8 squared is 64
Enter a number >6 #输入6
6 squared is 36
Enter a number >^Z # ctrl +z 是Windows下的文件结束符
Bye
接下来利用重定向的方式运行该脚本
>>> python test.py < input.txt
Hello stream world
Enter a number >8 squared is 64
Enter a number >6 squared is 36
Bye
利用重定向,无需交互式输入
同样,可以利用> filename
将输出重定向到文件
>>> python test.py > output.txt #仍需交互式输入,当outpu.txtt不存在时会在当前工作目录下自动创建
8
6
^Z
output.txt中内容如下
Hello stream world
Enter a number >8 squared is 64
Enter a number >6 squared is 36
Bye
也可以在一个命令中把输入输出重定向结合起来,如下所示
>>> python test.py < inpu.txt > output.txt
Hello stream world
Enter a number >8 squared is 64
Enter a number >6 squared is 36
Bye
获取调用者的函数名,两种方法
sys._getframe().f_back.f_code.co_name
# 或
sys._getframe(1).f_code.co_name
标签:None,python,bundle,sys,模块,._,pydevd,numpy 来源: https://blog.csdn.net/zangba9624/article/details/111620203