Courseiva
Modules and Packages →easyMultiple Choice

PCAP Modules and Packages Practice Question

A developer needs to add a custom directory '/home/user/mylibs' to Python's module search path so that modules in that directory can be imported. Which code snippet accomplishes this correctly?

⚠ Common exam trap

Many exam-takers confuse `sys.path` with a stack or set and choose a method like `push()` or `add()` that does not exist for Python lists, or they may incorrectly import a non-existent `path` module instead of `sys`.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

import sys; sys.path.append('/home/user/mylibs')

Python's module search path is stored in the list `sys.path`, and the standard way to add a custom directory at runtime is to use the `list.append()` method. This inserts the directory at the end of the search path, allowing modules in that directory to be imported after the standard library and site-packages directories.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    import sys; sys.path.append('/home/user/mylibs')

    Why this is correct

    This is the correct approach. The sys.path attribute is a list of directory strings that Python searches in order when resolving imports. list.append() is the standard method for adding a single element to the end of the list, so /home/user/mylibs becomes available for subsequent import statements, though only for the current process.

  • ✗

    import sys; sys.path.push('/home/user/mylibs')

    Why it's wrong here

    This code raises an AttributeError because list objects in Python do not define a push() method. Push is a stack operation, and while sys.path is mutable and ordered, its API is that of a plain list, which offers append() and insert() but not push(). The correct way to add an item to the end of a list is append(), not a backwards-compatible alias.

  • ✗

    import sys; sys.path.add('/home/user/mylibs')

    Why it's wrong here

    Although add() is a common method on set objects, it does not exist on Python lists, and sys.path is just a list. Calling sys.path.add(...) would raise AttributeError: 'list' object has no attribute 'add'. Lists use append() to add one item, or extend() to add multiple items; add() would be inappropriate even if it were defined because it suggests set semantics without preserving insertion order.

  • ✗

    import path; path.add('/home/user/mylibs')

    Why it's wrong here

    There is no standard library module named path that provides an add() function for altering sys.path. The Python standard library has modules like os.path and pathlib, but neither exposes a function named add() that modifies the import search list. Even if a third-party 'path' module existed, you would still need to import sys and mutate sys.path directly to affect the import machinery.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.