What Is "sys" Module

This section describes the 'sys' module, which allows you to access information from the Python system that executes the Python script.

What Is "sys" Module? "sys" is a Python module that allows you to access information from the Python system that executes the Python script.

Some commonly used properties and methods of the "sys" module are:

1. sys.argv - Property holds a list of command line arguments used to invoke this Python script. sys.argv[0] is the Python script file path. Below is an example on how to use sys.argv:

herong$ more argv_echoer.py
import sys
for i in range(len(sys.argv)):
  print("argv["+str(i)+"] = "+sys.argv[i])

herong$ python argv_echoer.py say "good morning"
argv[0] = argv_echoer.py
argv[1] = say
argv[2] = good morning

2. sys.copyright - Property holds the copyright information of the Python system.

>>> import sys
>>> print(sys.copyright)
Copyright (c) 2001-2019 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.

3. sys.executable - Property holds the executable program that launches the Python system.

>>> sys.executable
'/Library/Frameworks/Python.framework/Versions/3.8/bin/python3'

4. sys.getdefaultencoding() - Method returns the default encoding of characters.

>>> sys.getdefaultencoding()
'utf-8'

5. sys.getrefcount(object) - Method returns the numbers of references on the given object. The code below shows that a string literal has 3 references. I am not sure why.

>>> sys.getrefcount("Hello")
3

>>> msg = "Hello"
>>> sys.getrefcount(msg)
2

>>> ref = msg
>>> sys.getrefcount(ref)
3

6. sys.getsizeof(object) - Method returns the storage size of the given object. The code below shows that a string object has 49 bytes of overhead.

>>> msg = "Hello"
>>> sys.getsizeof(msg)
54
>>> len(msg)
5

>>> msg = "Hello Joe!"
>>> sys.getsizeof(msg)
59
>>> len(msg)
10

7. sys.modules - Property holds a dictionary of available modules.

>>> sys.modules.keys()
dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_warnings', ...
...

8. sys.path - Property holds a dictionary of available modules.

>>> sys.path
['',
 '/usr/local/anaconda3/lib/python39.zip',
 '/usr/local/anaconda3/lib/python3.9',
 '/usr/local/anaconda3/lib/python3.9/lib-dynload',
 '/usr/local/anaconda3/lib/python3.9/site-packages'
]

9. sys.platform - Property holds the platform name of the operating system.

>>> sys.platform
'linux'

10. sys.stdin, sys.stdout, and sys.stderr - Properties hold file handles for input, output and error processing.

>>> res = sys.stdout.write("Hello\n")
Hello

11. sys.version - Properties holds the version number of the Python system.

>>> sys.version
'3.9.12 (main, Apr  5 2022, 06:56:58) \n[GCC 7.5.0]'

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

"sys" and "os" Modules

What Is "sys" Module

 What Is "os" Module

 Use "os" Module to Create Child Processes

 Use "os" Module to Manage Files

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB