Last Updated : 21 Aug, 2024
Summarize
Comments
Improve
Sys is a built-in Python module that contains parameters specific to the system i.e. it contains variables and methods that interact with the interpreter and are also governed by it.
sys.path
sys.path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module.
When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules. If not found it looks through the list of directories(a directory is a folder that contains related modules) defined by sys.path.
Initializing sys.path
There are three ways to specify a path :
- DEFAULT- By default, the interpreter looks for a module within the current directory. To make the interpreter search in some other directory you just simply have to change the current directory. The following example depicts a default path taken by the interpreter:
# importing moduleimport sys# printing all directories for # interpreter to searchsys.path
Output:
- THROUGH ENVIRONMENT VARIABLES- An environment variable that contains the path an interpreter can take while looking for modules can be employed. Once set, it hints interpreter with directories to locate a module. The following example shows how this can be done.
PYTHONPATH=C:\Users\Vanshi\Desktop
# importing moduleimport sys# printing all directoriessys.path
Output:
- APPENDING PATH- append() is a built-in function of sys module that can be used with path variable to add a specific path for interpreter to search. The following example shows how this can be done.
# importing moduleimport sys# appending a pathsys.path.append('C:/Users/Vanshi/Desktop')# printing all pathssys.path
Output:
Note that the first string returned by path is always empty this is to indicate the interpreter to check in the current directory.
sys.path in Python – FAQs
What Does sys.path in Python?
sys.path
is a list in Python that contains the directories where the Python interpreter looks for modules to import. It includes the directory from which the script was run or the current directory if interactive, the list of directories specified by the environment variablePYTHONPATH
, and the installation-dependent default paths. Modifyingsys.path
at runtime allows dynamically adding directories where Python will look for modules.Example:
import sys
print(sys.path) # Outputs all paths where Python looks for modules
What is SYS in Python?
sys
is a module in Python that provides access to some variables used or maintained by the Python interpreter and functions that interact strongly with the interpreter. It is used for system-specific parameters and functions, such as command-line arguments (sys.argv
), the Python path (sys.path
), and the exit from Python (sys.exit()
).
How Do I Get the Current Path in SYS Python?
While
sys
does not provide a direct method to get the current working directory, this functionality is typically achieved using theos
module’sos.getcwd()
method. However,sys.path[0]
can sometimes be used to get the directory of the script that was initially executed.Example:
import os
current_path = os.getcwd()
print(current_path) # Outputs the current working directory
What is SYS exit() in Python?
sys.exit()
is a function in thesys
module that allows the developer to exit from Python. The optional argument passed tosys.exit()
indicates whether the program is terminating successfully (0
) or with an error (non-zero, typically1
, or another error message).Example:
import sys
sys.exit() # Exits Python cleanly
What Does sys.stdin Do in Python?
sys.stdin
refers to the input stream, which is used to read data inputted by the user during the execution of the program, usually via the keyboard or input from another program. It is used when you want to handle input more directly than functions likeinput()
allow.Example of reading a line from stdin:
import sys
line = sys.stdin.readline()
print(line)This method is more common in scripts that need to process input in a lower-level or more controlled way than
input()
provides, such as in cases where the script is reading from a pipeline