AppDividend
Latest Code Tutorials

Python sys path append() Method

0

The sys is a built-in Python module that contains parameters specific to the system. The sys.path contains a list of directories that the interpreter will search for the required module. In this article, we will see how to use the sys.path.append() method.

Python sys path append()

The sys.path.append() is a built-in function of the sys module in Python that can be used with path variables to add a specific path for an interpreter to search.

Syntax

sys.path.append(path)

Parameters

The sys.path.append() method takes a file path string as an argument.

Example

Let’s print the by default sys.path and then append a new path to the default and then print the new sys.path. To use the sys module, you need to import the sys module first.

import sys

print(sys.path)

sys.path.append("/Users/krunal/Desktop/code/pyt/database")

print("-----------------------")
print("After appending a path")
print("-----------------------")
# printing all paths
print(sys.path)

Output

['/Users/krunal/Desktop/code/pyt/database', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', 
 '/Users/krunal/Library/Python/3.9/lib/python/site-packages', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']
-----------------------
After appending a path
-----------------------
['/Users/krunal/Desktop/code/pyt/database', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', 
 '/Users/krunal/Library/Python/3.9/lib/python/site-packages', 
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages', 
 '/Users/krunal/Desktop/code/pyt/database']

The sys.path attribute always contains a listing of default paths.

Using the sys.path.append() method, and we added a new path to the default paths, as you can see in the last line of this output.

That is it for the sys.path.append() function in Python.

See also

Python sys.argv

Python os.system()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.