?print-pdf
' Created for
sys
modulesys
modulesys
is a built-in module, 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
- A string containing the version number of the Python interpreter plus additional information.sys.version_info
- A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial.
import sys
print(sys.version)
# 3.12.0 (main, Jan 2 2024, 16:26:01) [GCC 11.4.0]
print(sys.version_info)
# sys.version_info(major=3, minor=12, micro=0, releaselevel='final', serial=0)
import sys
if sys.version_info[0] == 2:
print("This program requires Python 3 or above. You are using Python 2.")
elif sys.version_info[0] == 3:
print("Python 3 is running")
sys.executable
- A string giving the absolute path of the executable binary for the Python interpreter
import sys
print(sys.executable)
#/usr/bin/python3
sys.path
- A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
import sys
for path in sys.path:
print(path)
#/media/user/data/projects/examples
#/usr/lib/python312.zip
#/usr/lib/python3.12
#/usr/lib/python3.12/lib-dynload
#/usr/local/lib/python3.12/dist-packages
#/usr/lib/python3/dist-packages
#/usr/lib/python3.12/dist-packages
sys.path
list:
import sys
# add 'my_modules' to path:
sys.path.append('/media/user/data/my_modules')
from module1 import foo
foo()
sys.getsizeof()
- Return the size of an object in bytes.sys.getsizeof()
function returns the size of the object itself, not the size of the elements it contains or references.
import sys
from functools import reduce
x = 1000
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
int_object_memory = sys.getsizeof(x)
list_object_memory = sys.getsizeof(l)
list_elements_memory = sum(sys.getsizeof(el) for el in l)
print(int_object_memory) # 28
print(list_object_memory) # 136
print(list_elements_memory) # 280
sys.exit([status])
- Terminate the script with an optional exit status.
import sys
sys.exit(0) # successful termination
sys.argv
: accessing command-line argumentssys.argv
: accessing command-line arguments
$ python greet_user.py --help
Usage: python greet_user.py [options]
Options:
--help Show this help message
--name User name (string)
--age User age (integer)
$ python greet_user.py --name=Ada --age=23
sys.argv
list:sys.argv[0] will always store the script name.
sys.argv[1:]
import sys
print(f"Script Name: {sys.argv[0]}")
print(f"Arguments: {sys.argv[1:]}")
###Output of: python greet_user.py --name=Ada --age=23
#Script Name: greet_user.py
#Arguments: ['--name=Ada', '--age=23']
import sys
print('Argument List:', sys.argv)
# call script without arguments:
$ python argv_examples.py
Argument List: ['argv_examples.py']
# call script with 1 argument:
$ python argv_examples.py arg1
Argument List: ['argv_examples.py', 'arg1']
# call script with 3 arguments:
$ python argv_examples.py arg1 23 -45.6
Argument List: ['argv_examples.py', 'arg1', '23', '-45.6']
import argparse
# Create the parser
parser = argparse.ArgumentParser(description='Example script demonstrating argparse usage.')
# define positional required argument:
parser.add_argument('message', help='the message to be repeated')
# define optional argument:
parser.add_argument('--repeat', type=int, default=1, help='How many times to repeat the message (default: %(default)s)')
# Parse arguments
args = parser.parse_args()
# Use arguments
for _ in range(args.repeat):
print(args.message)
###Output of: $ python echo.py hello --repeat 3
# hello
# hello
# hello
###Output of: $ python echo.py --help
# usage: echo.py [-h] [--repeat REPEAT] message
# Example script demonstrating argparse usage.
# positional arguments:
# message the message to be repeated
# options:
# -h, --help show this help message and exit
# --repeat REPEAT How many times to repeat the message (default: 1)
###Output of: python3 echo.py
# usage: echo.py [-h] [--repeat REPEAT] message
# echo.py: error: the following arguments are required: message
message
) that are required, and optional arguments (here, --repeat
) that the user can specify to alter the script's behavior.These slides are based on
customised version of
framework