Created for
sys
sys
sys
is a built-in package, which contains Python interpreter's specific parameters and functions
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '
_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'ba
se_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix
', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopen
flags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementa
tion', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path
_importer_cache', 'platform', 'prefix', 'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrac
e', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
sys.version
, sys.version_info
>>> import sys
>>> print(sys.version)
3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
>>> print(sys.version_info)
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
import sys
if sys.version_info[0] == 2:
print("Python 2 is running")
elif sys.version_info[0] == 3:
print("Python 3 is running")
$ python2.7 sys.version_info.py
Python 2 is running
$ python3.6 sys.version_info.py
Python 3 is running
sys.executable
>>> import sys
>>> print(sys.executable)
/usr/bin/python3
sys.path
>>> import sys
>>> for i in sys.path:
... print(i)
...
/home/nemsys/.local/bin
/usr/lib/python35.zip
/usr/lib/python3.5
/usr/lib/python3.5/plat-x86_64-linux-gnu
/usr/lib/python3.5/lib-dynload
/home/nemsys/.local/lib/python3.5/site-packages
/usr/local/lib/python3.5/dist-packages
/usr/lib/python3/dist-packages
sys.getsizeof()
>>> import sys
>>>
>>> print(sys.getsizeof(2))
28
>>> print(sys.getsizeof(range(1)))
48
print(sys.getsizeof(range(1000000)))
48
>>> print(sys.getsizeof([1,2,3,4,5]))
104
>>> print(sys.getsizeof((1,2,3,4,5)))
88
>>> print(sys.getsizeof("a"))
50
>>> print(sys.getsizeof("abcabc"))
55
import sys
# print(sys.argv)
user_name = sys.argv[1]
print("Hello {}, how are you today?".format(user_name))
$ python argv_demo.py pesho
sys.argv[0] will store the script name.
import sys
print('Argument List:', sys.argv)
$ python argv_examples.py
Argument List: ['argv_examples.py']
$ python argv_examples.py arg1
Argument List: ['argv_examples.py', 'arg1']
$ python argv_examples.py arg1 arg2
Argument List: ['argv_examples.py', 'arg1', 'arg2']
import sys
sum = 0
# get only arguments, without the script name
args = sys.argv[1:]
for arg in args:
sum += int(arg)
print ("The sum of {} is {}".format(args, sum))
python sum_args.py 1 2 3
The sum of ['1', '2', '3'] is 6
These slides are based on
customised version of
framework