Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Learn Python Sys Module - DesignLinux

May 29 2020
designlinux 0 Comments

In this article, we will take a look at the Python Sys Module. There are variables and functions that are maintained by the interpreter and the sys module provides a way of interacting with them. These variables are available until the interpreter is alive. We will have a glance at some of the commonly used sys functions.

To work with the sys module you have to first import the module.

sys.version – This stores the information about the current version of python.

$ python3
>>> import sys
>>> sys.version
Show Current Python Version

Show Current Python Version

sys.path – Path variable stores the directory path in the form of a list of strings. Whenever you import a module or run a program using a relative path, python interpreter search for the necessary module or script using the path variable.

Path index stores the directory containing the script that was used to invoke the Python interpreter at the index “Zero”. If the interpreter is invoked interactively or if the script is read from standard input, path[0] will be an empty string.

>>> sys.path
Invoking in Interpreter

Invoking in Interpreter

When invoking the script the path[0] stores the directory path.

$ vim 1.py
$ python3 1.py
Invoking as Script

Invoking as Script

If you have modules in a custom directory then you can add the directory path to the path variable using a path.append() method (since the path is a list object we are using the list method “append”).

$ python3
>>> import sys
>>> sys.path
>>> sys.path.append('/root/test/')
>>> sys.path
Python Append Method

Python Append Method

sys.argv – argv is used to pass run time arguments to your python program. Argv is a list that stores the script name as the 1st value followed by the arguments we pass. Argv values are stored as type string and you have to explicitly convert it according to your needs.

>>> sys.argv

When you run below snippet, the end value of range function is passed via sys.argv[1] as 10 and few other values are also passed to print the list of argv values at the end of the program.

#!/usr/bin/python3

import sys

for x in range(1,int(sys.argv[1])):
    print(x)
    
# Print all the arguments passed
print("Arguments passed:",sys.argv)
Passing Arguments in Python

Passing Arguments in Python

sys.executable – Prints the absolute path of the python interpreter binary.

>>> sys.executable
'/usr/bin/python3'

sys.platform – Prints the os platform type. This function will be very useful when you run your program as a platform dependent.

>>> sys.platform
'linux'

sys.exit – Exit the interpreter by raising SystemExit(status). By default, status is said to be Zero and is said to be successful. We can either use an integer value as Exit Status or other kinds of objects like string(“failed”) as shown in the below example.

Below the sample, a snippet is used to check if the platform is windows and then run the code. If not raise exit() function.

#!/usr/bin/python3

import sys

if sys.platform == 'windows':  # CHECK ENVIRONMENT
    #code goes here
    pass
else:
    print("This script is intended to run only on Windows, Detected platform: ", sys.platform)
    sys.exit("Failed")
Check OS Platform

Check OS Platform

sys.maxsize – This is an integer value representing maximum value a variable can hold.

On a 32-bit platform it is 2**31 - 1 
On a 64-bit platform it is 2**63 - 1
Wrapup

We have seen some of the important functions of the sys module and there are a lot more functions. Until we come up with the next article you can read more about the sys module here.

Sharing is Caring…
Share on FacebookShare on TwitterShare on LinkedinShare on Reddit

Related

Tags: online, Python Data Structure

How to Install KVM on CentOS/RHEL 8

Prev Post

How to Install Joomla on Ubuntu 18.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 256
  • 614,628

DesignLinux.com © All rights reserved

Go to mobile version