Question 160 of 511
Modules and PackagesmediumMultiple SelectObjective-mapped

Quick Answer

The correct answer is that both from pkg.bar import foo and from pkg import bar followed by bar.foo are valid ways to import a function from a subpackage. The first method directly binds the function foo into the current namespace by specifying the full dotted path from the package to the module, which is the most explicit and commonly used approach. The second method works because importing the module bar from pkg makes bar an attribute of pkg, and then foo is accessed as an attribute of that module object, which Python resolves at runtime. On the PCAP exam, this tests your understanding of Python’s import system and the distinction between importing a module versus importing a specific name from it. A common trap is assuming that from pkg import bar.foo is valid syntax, but Python does not allow chaining attributes after the import keyword. For a quick memory tip, remember that the dot in an import statement always stops at the last module or package name; anything after that must be accessed as an attribute.

PCAP Modules and Packages Practice Question

This PCAP practice question tests your understanding of modules and packages. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

Which TWO methods are valid ways to import a function named 'foo' from a module 'bar' that is part of a package 'pkg'?

Question 1mediummulti select
Full question →

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

from pkg.bar import foo

Option B is correct because the 'from pkg.bar import foo' syntax directly imports the 'foo' function from the 'bar' submodule within the 'pkg' package, making 'foo' available in the current namespace. Option D is also correct because it first imports the 'bar' module from 'pkg' using 'from pkg import bar', then accesses 'foo' as an attribute of 'bar' (bar.foo), which is a valid two-step approach.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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 bar

    Why it's wrong here

    Ignores the package; would import a top-level bar module if exists, not pkg.bar.

  • from pkg.bar import foo

    Why this is correct

    Correct absolute import.

    Related concept

    Read the scenario before looking for a memorised answer.

  • import pkg; pkg.bar.foo

    Why it's wrong here

    Requires pkg.bar to be loaded, which is not done by 'import pkg' alone unless imported elsewhere.

  • from pkg import bar; bar.foo

    Why this is correct

    Works after importing the module.

    Related concept

    Read the scenario before looking for a memorised answer.

  • import pkg.bar.foo

    Why it's wrong here

    Invalid syntax; function cannot be imported directly like that.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between importing a module versus importing an attribute from a module, and the trap here is that candidates mistakenly think 'import pkg.bar.foo' (Option E) is valid syntax, when in fact you can only import modules or packages with the dotted-path import statement, not functions or classes.

Detailed technical explanation

How to think about this question

In Python, packages are directories containing an __init__.py file, and submodules are .py files within that directory. The 'from pkg.bar import foo' syntax triggers Python's import machinery to locate 'pkg/bar.py', execute it, and bind the name 'foo' from its namespace into the current scope. The two-step approach in D works because 'from pkg import bar' adds 'bar' to the local namespace as a module object, and then 'bar.foo' accesses the attribute from that module. A common real-world scenario is when you want to avoid namespace pollution or when you need to use the module object for dynamic attribute access.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A company's IT admin needs to give a contractor read-only access to production logs without sharing account credentials. Using role-based access control (RBAC) and temporary scoped permissions — not a permanent shared password — is the correct pattern. Questions like this test whether you can apply least-privilege access across cloud identity services.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Modules and Packages — This question tests Modules and Packages — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: from pkg.bar import foo — Option B is correct because the 'from pkg.bar import foo' syntax directly imports the 'foo' function from the 'bar' submodule within the 'pkg' package, making 'foo' available in the current namespace. Option D is also correct because it first imports the 'bar' module from 'pkg' using 'from pkg import bar', then accesses 'foo' as an attribute of 'bar' (bar.foo), which is a valid two-step approach.

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more ways this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which THREE of the following are valid ways to import a function named 'calculate' from a module named 'math_ops' located in a subpackage 'operations' of a package 'app'?

hard
  • A.from app.operations.math_ops import calculate
  • B.import importlib; module = importlib.import_module('app.operations.math_ops'); module.calculate()
  • C.import app.operations.math_ops.calculate
  • D.from app.import operations.math_ops import calculate
  • E.import app.operations.math_ops; app.operations.math_ops.calculate()

Why A: Option A is correct because it uses the standard Python import syntax to directly import the 'calculate' function from the 'math_ops' module, which is located in the 'operations' subpackage of the 'app' package. This is the most straightforward and recommended way to import a specific attribute from a module.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.