Python sys.path Attribute

Vaibhav Vaibhav Jan 30, 2023
  1. Syntax of Python sys.path Attribute
  2. Example 1: Use sys.path to Get All the Paths
  3. Example 2: Use sys.path to Add a Path to Search for Modules
Python sys.path Attribute

The sys module is associated with the Python interpreter. It offers various functions that help interact with the Python interpreter, and several variables are maintained and used by the interpreter.

Since it is a built-in module, it is available by default. This module owns a variable, path, that stores a list of file system paths to search for Python modules.

Syntax of Python sys.path Attribute

sys.path

Parameters

Since it is a variable, not a function, it has no parameters.

Returns

Since it is a variable and not a function, it does not return anything. It is a regular Python list of strings, and strings can be accessed using valid indexes.

Example 1: Use sys.path to Get All the Paths

import sys

print(sys.path)

Output:

[
    '/home/repl/13621cdd-4388-4f56-8ddf-a3fdc618c835',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/usr/lib/python3.8/site-packages'
]

Note that the output will differ from system to system. By default, the first item in the list is the file system path to the current working directory.

If the above code is executed from an interactive Python shell, this item will be an empty string because the script is not saved anywhere.

Example 2: Use sys.path to Add a Path to Search for Modules

import sys

path = "/home/user/python/modules/"
sys.path.append(path)
print(sys.path)

Output:

[
    '/home/repl/39dbc83d-a54b-47e8-b38c-93afc904104d',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/usr/lib/python3.8/site-packages',
    '/home/user/python/modules/'
]

Imagine that we wrote some Python APIs and modules on our own and stored them in some folder. Now we need to use those modules in some projects.

A straightforward solution would be to copy the files in the current directory and use it. Now suppose we did this for 10 projects.

A few weeks later, we updated the original library and to apply these changes to every copy; we have to change the files at all 10 locations. This, indeed, is a tedious and efficient approach.

What if we consider that folder with all the modules as a source and tell the Python interpreter to look for modules in this directory? That exactly is what sys.path.append() lets us do.

We can add our paths to sys.path and ask the Python interpreter to look for modules at those paths.

Considering there are no name clashes across modules, this is a fabulous approach because now we have to write updates only in one place, and they are accessible to every project using it. Also, one can easily incorporate version control using Git.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python Sys