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\\’, ‘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\\’>, ‘encodings.
latin_1′: <module ‘encodings.latin_1’ from ‘D:\\Python33\\lib\\encodings\\latin_’>, ‘os.path’: <module ‘ntpath’ from ‘D:\\Python33\\lib\\’>, ‘bisec
t’: <module ‘bisect’ from ‘D:\\Python33\\lib\\’>, ‘codecs’: <module ‘co
decs’ from ‘D:\\Python33\\lib\\’>, ‘encodings.cp852’: <module ‘encoding
s.cp852′ from ‘D:\\Python33\\lib\\encodings\\’>, ‘_heapq’: <module ‘_hea
pq’ (built-in)>, ‘’: <module ‘’ from ‘D:\\Python33
\\lib\\collections\\’>, ‘encodings.mbcs’: <module ‘encodings.mbcs’ from ‘D
:\\Python33\\lib\\encodings\\’>, ‘abc’: <module ‘abc’ from ‘D:\\Python33\
\lib\\’>, ‘keyword’: <module ‘keyword’ from ‘D:\\Python33\\lib\\
‘>, ‘ntpath’: <module ‘ntpath’ from ‘D:\\Python33\\lib\\’>, ‘encodings.
utf_8′: <module ‘encodings.utf_8’ from ‘D:\\Python33\\lib\\encodings\\’>
, ‘zipimport’: <module ‘zipimport’ (built-in)>, ‘_warnings’: <module ‘_warnings’
 (built-in)>, ‘site’: <module ‘site’ from ‘D:\\Python33\\lib\\’>, ‘_funct
ools’: <module ‘_functools’ (built-in)>, ‘stat’: <module ‘stat’ from ‘D:\\Python
33\\lib\\’>, ‘_bisect’: <module ‘_bisect’ (built-in)>, ‘_weakrefset’: <mo
dule ‘_weakrefset’ from ‘D:\\Python33\\lib\\’>, ‘os’: <module ‘os’
 from ‘D:\\Python33\\lib\\’>, ‘_codecs’: <module ‘_codecs’ (built-in)>, ‘ma
rshal’: <module ‘marshal’ (built-in)>, ‘collections’: <module ‘collections’ from
 ‘D:\\Python33\\lib\\collections\\’>, ‘__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’
: <module ‘_locale’ (built-in)>, ‘winreg’: <module ‘winreg’ (built-in)>, ‘sys’:
<module ‘sys’ (built-in)>, ‘sre_constants’: <module ‘sre_constants’ from ‘D:\\Py
thon33\\lib\\’>, ‘_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\\’>, ‘heapq’
: <module ‘heapq’ from ‘D:\\Python33\\lib\\’>, ‘encodings’: <module ‘enc
odings’ from ‘D:\\Python33\\lib\\encodings\\’>, ‘sre_compile’: <modul
e ‘sre_compile’ from ‘D:\\Python33\\lib\\’>, ‘sysconfig’: <module
‘sysconfig’ from ‘D:\\Python33\\lib\\’>, ‘re’: <module ‘re’ from ‘D:
\\Python33\\lib\\’>, ‘functools’: <module ‘functools’ from ‘D:\\Python33\\l
ib\\’>, ‘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\\’>, ‘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\\’>,
 ‘encodings.cp1250’: <module ‘encodings.cp1250’ from ‘D:\\Python33\\lib\\encodin
gs\\’>, ‘genericpath’: <module ‘genericpath’ from ‘D:\\Python33\\lib\\g’>}

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

>>> sys.platform

>>> sys.copyright
‘Copyright (c) 2001-2012 Python Software Foundation.\nAll Rights Reserved.\n\nCo
pyright (c) 2000\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

>>> 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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s