sys Module in Python

sys Module in Python

This is a very important module. We have to import it first:

>>> import sys

>>> dir(sys)
[‘__displayhook__’, ‘__doc__’, ‘__excepthook__’, ‘__loader__’, ‘__name__’, ‘__pa
ckage__’, ‘__stderr__’, ‘__stdin__’, ‘__stdout__’, ‘_clear_type_cache’, ‘_curren
t_frames’, ‘_debugmallocstats’, ‘_getframe’, ‘_home’, ‘_mercurial’, ‘_xoptions’,
 ‘api_version’, ‘argv’, ‘base_exec_prefix’, ‘base_prefix’, ‘builtin_module_names
‘, ‘byteorder’, ‘call_tracing’, ‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhan
dle’, ‘dont_write_bytecode’, ‘exc_info’, ‘excepthook’, ‘exec_prefix’, ‘executabl
e’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, ‘getcheckinterval’, ‘getd
efaultencoding’, ‘getfilesystemencoding’, ‘getprofile’, ‘getrecursionlimit’, ‘ge
trefcount’, ‘getsizeof’, ‘getswitchinterval’, ‘gettrace’, ‘getwindowsversion’, ‘
hash_info’, ‘hexversion’, ‘implementation’, ‘int_info’, ‘intern’, ‘last_tracebac
k’, ‘last_type’, ‘last_value’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’,
‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘ps1’, ‘ps2’,
 ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘se
ttrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘version’, ‘version_info’,
‘warnoptions’, ‘winver’]

There are a lot of useful methods. We should know the most important of them.

>>> sys.path
[”, ‘D:\\Python33\\scripts’, ‘C:\\WINDOWS\\system32\\python33.zip’, ‘D:\\Python
33\\DLLs’, ‘D:\\Python33\\lib’, ‘D:\\Python33’, ‘D:\\Python33\\lib\\site-package
s’, ‘D:\\Python33\\lib\\site-packages\\win32’, ‘D:\\Python33\\lib\\site-packages
\\win32\\lib’, ‘D:\\Python33\\lib\\site-packages\\Pythonwin’]
>>>

Yes, we see all what is in the Python PATH.

>>> sys.modules
{‘reprlib’: <module ‘reprlib’ from ‘D:\\Python33\\lib\\reprlib.py’>, ‘encodings.
latin_1′: <module ‘encodings.latin_1’ from ‘D:\\Python33\\lib\\encodings\\latin_
1.py’>, ‘os.path’: <module ‘ntpath’ from ‘D:\\Python33\\lib\\ntpath.py’>, ‘bisec
t’: <module ‘bisect’ from ‘D:\\Python33\\lib\\bisect.py’>, ‘codecs’: <module ‘co
decs’ from ‘D:\\Python33\\lib\\codecs.py’>, ‘encodings.cp852’: <module ‘encoding
s.cp852′ from ‘D:\\Python33\\lib\\encodings\\cp852.py’>, ‘_heapq’: <module ‘_hea
pq’ (built-in)>, ‘collections.abc’: <module ‘collections.abc’ from ‘D:\\Python33
\\lib\\collections\\abc.py’>, ‘encodings.mbcs’: <module ‘encodings.mbcs’ from ‘D
:\\Python33\\lib\\encodings\\mbcs.py’>, ‘abc’: <module ‘abc’ from ‘D:\\Python33\
\lib\\abc.py’>, ‘keyword’: <module ‘keyword’ from ‘D:\\Python33\\lib\\keyword.py
‘>, ‘ntpath’: <module ‘ntpath’ from ‘D:\\Python33\\lib\\ntpath.py’>, ‘encodings.
utf_8′: <module ‘encodings.utf_8’ from ‘D:\\Python33\\lib\\encodings\\utf_8.py’>
, ‘zipimport’: <module ‘zipimport’ (built-in)>, ‘_warnings’: <module ‘_warnings’
 (built-in)>, ‘site’: <module ‘site’ from ‘D:\\Python33\\lib\\site.py’>, ‘_funct
ools’: <module ‘_functools’ (built-in)>, ‘stat’: <module ‘stat’ from ‘D:\\Python
33\\lib\\stat.py’>, ‘_bisect’: <module ‘_bisect’ (built-in)>, ‘_weakrefset’: <mo
dule ‘_weakrefset’ from ‘D:\\Python33\\lib\\_weakrefset.py’>, ‘os’: <module ‘os’
 from ‘D:\\Python33\\lib\\os.py’>, ‘_codecs’: <module ‘_codecs’ (built-in)>, ‘ma
rshal’: <module ‘marshal’ (built-in)>, ‘collections’: <module ‘collections’ from
 ‘D:\\Python33\\lib\\collections\\__init__.py’>, ‘__main__’: <module ‘__main__’
(built-in)>, ‘_collections’: <module ‘_collections’ (built-in)>, ‘builtins’: <mo
dule ‘builtins’>, ‘copyreg’: <module ‘copyreg’ from ‘D:\\Python33\\lib\\copyreg.
py’>, ‘locale’: <module ‘locale’ from ‘D:\\Python33\\lib\\locale.py’>, ‘_locale’
: <module ‘_locale’ (built-in)>, ‘winreg’: <module ‘winreg’ (built-in)>, ‘sys’:
<module ‘sys’ (built-in)>, ‘sre_constants’: <module ‘sre_constants’ from ‘D:\\Py
thon33\\lib\\sre_constants.py’>, ‘_weakref’: <module ‘_weakref’ (built-in)>, ‘_f
rozen_importlib’: <module ‘_frozen_importlib’ from ‘<frozen>’>, ‘_sre’: <module
‘_sre’ (built-in)>, ‘_imp’: <module ‘_imp’ (built-in)>, ‘signal’: <module ‘signa
l’>, ‘weakref’: <module ‘weakref’ from ‘D:\\Python33\\lib\\weakref.py’>, ‘heapq’
: <module ‘heapq’ from ‘D:\\Python33\\lib\\heapq.py’>, ‘encodings’: <module ‘enc
odings’ from ‘D:\\Python33\\lib\\encodings\\__init__.py’>, ‘sre_compile’: <modul
e ‘sre_compile’ from ‘D:\\Python33\\lib\\sre_compile.py’>, ‘sysconfig’: <module
‘sysconfig’ from ‘D:\\Python33\\lib\\sysconfig.py’>, ‘re’: <module ‘re’ from ‘D:
\\Python33\\lib\\re.py’>, ‘functools’: <module ‘functools’ from ‘D:\\Python33\\l
ib\\functools.py’>, ‘itertools’: <module ‘itertools’ (built-in)>, ‘encodings.ali
ases’: <module ‘encodings.aliases’ from ‘D:\\Python33\\lib\\encodings\\aliases.p
y’>, ‘operator’: <module ‘operator’ (built-in)>, ‘io’: <module ‘io’ from ‘D:\\Py
thon33\\lib\\io.py’>, ‘nt’: <module ‘nt’ (built-in)>, ‘_io’: <module ‘io’ (built
-in)>, ‘_thread’: <module ‘_thread’ (built-in)>, ‘errno’: <module ‘errno’ (built
-in)>, ‘sre_parse’: <module ‘sre_parse’ from ‘D:\\Python33\\lib\\sre_parse.py’>,
 ‘encodings.cp1250’: <module ‘encodings.cp1250’ from ‘D:\\Python33\\lib\\encodin
gs\\cp1250.py’>, ‘genericpath’: <module ‘genericpath’ from ‘D:\\Python33\\lib\\g
enericpath.py’>}
>>>

We see our modules in our Python and where they are. Sometimes it’s important to know that.

>>> sys.platform
‘win32’
>>>

>>> sys.copyright
‘Copyright (c) 2001-2012 Python Software Foundation.\nAll Rights Reserved.\n\nCo
pyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Co
rporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright
(c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.’
>>>

>>> sys.prefix
‘D:\\Python33’
>>>

>>> sys.ps1
‘>>> ‘
>>> sys.ps2
‘… ‘
>>>

We can see our prompts.

>>> sys.version
‘3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)]’

>>>

>>> sys.version_info
sys.version_info(major=3, minor=3, micro=0, releaselevel=’final’, serial=0)
>>>

>>> sys.stderr
<_io.TextIOWrapper name='<stderr>’ mode=’w’ encoding=’cp852′>
>>> sys.stdin
<_io.TextIOWrapper name='<stdin>’ mode=’r’ encoding=’cp852′>
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>’ mode=’w’ encoding=’cp852′>
>>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s