Sys Module with Examples in Python

importox

importox

Posted on June 9, 2020

Sys Module with Examples in Python

The sys module has access to some variables used by the interpreter.

In this article you'll see some sys module examples in the Python programming language.

Python sys Module

The sys module provides you with details about the Python interpreter. Note that if you want to do things with the operating system like running a program you can use the os module instead.

This module is one of the best Python modules, and if you want to use the sys module, you need to import it into the program like other built-in modules.

python sys module

This provides a lot of information about the Python interpreter, such as the version of the interpreter, the maximum value that the variable can hold, the copyright information of the Python interpreter, etc.

Here, with an example, you can see some important functions of the sys module.

You can try this from both the shell and from a script.

Important functions of the sys module are shown below.

1) sys.version

This function returns the version of the Python interpreter in string format with the date of the installation.

Python Example:

    # Importing the module
    import sys

    # Printing the python version
    print(sys.version)
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

3.7.7 (default, Mar 13 2020, 10:23:39) \n[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
Enter fullscreen mode Exit fullscreen mode

2) sys.copyright

This function returns information about the copyright of the Python interpreter.

Python Example:

    # Importing the sys module
    import sys

    # Printing copyright info
    print('Information about copyright:')
    print(sys.copyright)
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

'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.'
Enter fullscreen mode Exit fullscreen mode

3) sys.maxsize

This function returns the maximum value of int that a variable can take.

Python Example:

    # Importing the sys module
    import sys

    # Printing the max value of an int
    print('Maximum value of an int:',sys.maxsize)
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

Maximum value of an int: 9223372036854775807
Enter fullscreen mode Exit fullscreen mode

sys.path

This function returns a list which includes all Python modules in the search path.

Python Example:

    # Importing the sys module
    import sys

    # Printing the all path
    print('List of the all paths:')
    print(sys.path)
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

List of the all path: ['/home/frank', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/home/frank/.local/lib/python3.7/site-packages', '/home/frank/.local/lib/python3.7/site-packages/tpass-0.1.8-py3.7.egg', '/home/frank/.local/lib/python3.7/site-packages/simplejson-3.17.0-py3.7.egg', '/home/frank/.local/lib/python3.7/site-packages/pyperclip-1.8.0-py3.7.egg', '/usr/lib/python3.7/site-packages', '/usr/lib64/python3.7/site-packages']
Enter fullscreen mode Exit fullscreen mode

Read more

💖 💪 🙅 🚩
importox
importox

Posted on June 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related