Menu Close

Python sys module

In this article, we will go through the Python sys module.sys module in Python is a built-in module that is used in different parts of the Python runtime environment.

In previous tutorials, we have seen lots of Python built-in functions along with examples.
Before going through this article, you should have basic knowledge of Python. Click here to learn all about Python.

Python sys module

Python sys module is a built-in module that comes with Python. Sys module in Python provides lots of attributes and methods that are used to manipulate the python run time environment. Here we will learn some important features of the Python sys module using examples.

To use the sys module in Python script, you have to import it by using the import keyword.

sys.argv

sys.args returns the list of Python command-line arguments that pass to the Python script.
All the arguments can access by using their index number. argv[0] represents the python script file name.

We have a test.py python file that contains following python script.


import sys
print("Your entered arguments are:- ", sys.argv[0], sys.argv[1], sys.argv[2])

Output


C:\Users\Vishvajit\Desktop\command-line>python test.py programming funda
Your entered arguments are:-  test.py programming funda

sys.base_exec_prefix

sys.base_exec_prefix returns the whole Python path which set before the python script file name.


C:\Users\Vishvajit\Desktop\command-line>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.base_exec_prefix
'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39'

sys.builtin_module_names

sys.builtin_module_names returns all built-in module that is compiled into this Python interpreter.


C:\Users\Vishvajit\Desktop\command-line>python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.builtin_module_names
('_abc', '_ast', '_bisect', '_blake2', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_contextvars', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_peg_parser', '_pickle', '_random', '_sha1', '_sha256', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_statistics', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', '_xxsubinterpreters', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zlib')

sys.copyright

sys.copyright returns the copyright version of current python version.


import sys
sys.copyright
'Copyright (c) 2001-2020 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.'

sys.call_tracing(func, args)

sys.call_tracing is used to call the function along with arguments.


import sys
def Addition(a, b):
    print(a + b)

sys.call_tracing(Addition, (6,2))

The output will be 8.

sys.executable

sys.executable returns the exe file of Python.


>>> import sys
>>> sys.executable
'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39\\python.exe'

sys.maxsize

sys.maxsize returns the largest integer of a variable.


>>> import sys
>>> sys.maxsize
9223372036854775807

sys.path

sys.path returns the list of environment variables that are searching for all the Python modules.


>>> import sys
>>> sys.path
['', 'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39', 'C:\\Users\\Vishvajit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']

sys.version

sys.version returns the string containing the current version of python interpreter.


>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]'

sys.platform

sys.platform returns the platform on which are working.


>>> import sys
>>> sys.platform
'win32'

sys.stdin

sys.stdin is used to take input from the user.


import sys
user_input = sys.stdin.readline()
print(user_input)

Output


C:\Users\Vishvajit\Desktop\command-line>python test.py
programming funda
programming funda

Conclusion

In this article, you have learned all about the Python sys module that is used to modify the different parts of the python runtime environment.
Sys module in python is very useful when you are working with making python a command-line application.

If you like this Python sys module article, please share and keep visiting for further Python tutorials.

Python built-in modules


For more information:- Click Here

Python self parameter
Python File Write

Related Posts