Python SYS Module

28/12/2020

In this lesson on Python sys module, we will study how this module allows us to interact with the interpreter and the host machine directly. Let’s see what all features this module offers to us.

Python sys module

The first advantage the sys module offers to us is its independence from the host machine Operating System. This means that this module can work the same even if it is working on Windows or Macintosh or Linux or any given OS. In this post, we will study the functions it provides to us. Let’s get started.

Working with sys module

To start working with the sys module and make scripts use this module, we will use the same import statement in all the scripts we write:

import sys

This statement imports and brings required sys module dependencies into our scope.

Python sys.argv

The most basic operation sys module allow us is to access the command line parameters passed to the script. Let’s write a program to demonstrate this:

import sys

print(‘Passed Command line parameters:’)
for param in sys.argv:
print(param)

When we run this program with command line parameters, we will see this output:

Note how this picked even the file name we passed to it as a command line parameter.

Python sys.path

Using the path function, we can display the PYTHONPATH which is provided in the host machine. We will write a script to use this function now:

import sys
print(n PYTHONPATH: ‘, sys.path)

When we run this program, we will see this output:

Of course, this can vary based on your machine.

Python sys.stdin

We can use the sys module to take input from the user as well. We will try this function now:

import sys

input_by_user = sys.stdin.readline()
print("User Input : " + input_by_user)

When we run this program, we will see this output:

This is very important function which we will usually use in our programs to take user input.

Python sys.copyright

To display the copyright information related to currently installed Python version on the host machine, we can use this function:

import sys
print(sys.copyright)

When we run this program, we will see this output:

Python sys.exit

There are many use cases when we must choose to exit the program flow without doing anything else (like error handling etc.). This is easy to achieve using the exit function call:

import sys

print("Hello Linuxhint")
sys.exit(1)
print("Not printed!")

When we run this program, we will see this output:

In this quick post on Python, we saw how we can use various functions provided by sys module in Python and demonstrated them with example scripts. Go on and play with more of them.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Logging Into Websites With Python

The login feature is an important functionality in today’s web applications. This feature helps keep special content...
29/12/2020

How to Create and Manage Python Virtual Environments

It is pretty common to see Python developers installing and upgrading packages from standard and non-standard sources to...
29/12/2020

How to Install Anaconda Python on Ubuntu 18.04 LTS

Anaconda Python is a Python distribution just like Ubuntu is a Linux distribution. Anaconda Python comes pre-installed...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025