PCAP Modules and Packages • Complete Question Bank
Complete PCAP Modules and Packages question bank — all 0 questions with answers and detailed explanations.
A developer creates a package named 'analytics' with the following structure:
analytics/ __init__.py stats.py models/ __init__.py regression.py
The developer wants the statement 'from analytics import *' to import only the functions 'mean' and 'std' from stats.py. What should be added to analytics/__init__.py?
You are a developer at a company that builds a data processing pipeline. The pipeline consists of several Python modules organized in a package called 'pipeline'. The package structure is:
pipeline/ __init__.py load.py transform.py analyze.py
The pipeline is deployed on a server where Python 3.8 is installed. The server also has a globally installed package called 'pipeline' (from a different project) in the site-packages directory. When you run your scripts that import 'pipeline', you get unexpected behavior because Python is importing the wrong package. You need to ensure that your local 'pipeline' package is used instead of the global one. You cannot uninstall the global package because it is used by another application. You have the following options:
A) Modify the PYTHONPATH environment variable to include the directory containing your 'pipeline' package before the site-packages directory. B) Rename your local 'pipeline' package to something else and update all imports. C) Use a virtual environment specific to your project and install your package there. D) Add an __init__.py file with a special import hook to override the global package.
Which course of action is the most appropriate and reliable?
A Python package 'mypackage' contains the following hierarchy:
mypackage/ __init__.py subpackage1/ __init__.py module_a.py subpackage2/ __init__.py module_b.py
From a script outside the package, a programmer writes:
import mypackage.subpackage1.module_a
Which statement is true about the import?
A developer has a module 'config.py' with the following content:
# config.py import os
DATABASE_URL = os.getenv('DATABASE_URL', 'localhost')
Another module 'app.py' imports config and uses DATABASE_URL. During testing, the environment variable is set correctly, but the import still uses the default value 'localhost'. What is the most likely reason?
A Python package 'shapes' has the following __init__.py:
# shapes/__init__.py from .circle import Circle from .square import Square
A user writes:
import shapes
c = shapes.Circle(5)
This works correctly. However, when the user writes:
import shapes.circle
c = shapes.circle.Circle(5)
It fails with AttributeError: module 'shapes' has no attribute 'circle'. What is the most likely reason?
You are working on a Python-based data processing pipeline that runs on a Linux server. The pipeline consists of several custom packages and modules located in /opt/mypipeline. The directory structure includes:
/opt/mypipeline/ __init__.py core/ __init__.py processor.py utils/ __init__.py logger.py
The main entry point is /opt/mypipeline/run.py, which imports modules from core and utils. The pipeline is executed using the command:
python /opt/mypipeline/run.py
Recently, the system administrator added a new Python package 'external_lib' to /opt/external_lib, and updated the PYTHONPATH environment variable to include /opt/external_lib. However, after this change, the pipeline fails to start with the following error:
ImportError: cannot import name 'process' from 'core.processor' (unknown location)
You check the PYTHONPATH and find it contains:
/opt/mypipeline:/opt/external_lib
The 'core.processor' module defines a function 'process' that is imported in run.py as:
from core.processor import process
The 'core' package has an __init__.py file. The 'external_lib' package also has a subpackage named 'core' with an __init__.py. What is the most likely cause of the import error?
Refer to the exhibit.
```
# file: mypackage/__init__.py
from . import module1
from .module2 import func2
__all__ = ['module1', 'func2']
```
```
# file: mypackage/module2.py
def func2():
print('func2')
```
```python
>>> from mypackage import *
>>> dir()
['__builtins__', ...]
```Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
Returns the length of an object
Generates a sequence of numbers
Returns a sorted list from an iterable
Returns index and value pairs
Aggregates elements from multiple iterables
Drag a concept onto its matching description — or click a concept then click the description.
8
3
1
15
14
Exhibit: # file: mypackage/__init__.py __all__ = ["module_a", "module_b"] # file: mypackage/module_a.py def func(): pass # file: mypackage/module_b.py def func(): pass
Exhibit: $ pip list Package Version ---------- ------- numpy 1.24.0 pandas 1.5.3 pip 22.0.4
Exhibit:
Project structure:
main.py
utils/
__init__.py
helpers.py
strings/
__init__.py
format.py
# main.py
from utils.helpers import greet
from utils.strings.format import boldA developer runs the following code in a script that is not part of a package:
import sys
sys.path.insert(0, '/custom/path')
import mymodule print(mymodule.__name__)
What is the output if 'mymodule' is found at '/custom/path/mymodule.py'?
Traceback (most recent call last):
File "script.py", line 1, in <module>
from mypackage import myfunction
ModuleNotFoundError: No module named 'mypackage'ERROR: Could not find a version that satisfies the requirement package==1.0 (from versions: 2.0, 2.1) ERROR: No matching distribution found for package==1.0
Consider the following package structure:
mypackage/
__init__.py
module_a.py
subpackage/
__init__.py
module_b.pymy_project/
__init__.py
app.py
utils/
__init__.py
helpers.py (contains def greet(): pass)Traceback (most recent call last):
File "main.py", line 1, in <module>
from mypackage import mymodule
ImportError: cannot import name 'mymodule' from 'mypackage' (unknown location)Refer to the exhibit. A Python script uses the following code to load the policy. However, it fails with a JSONDecodeError. What is the most likely cause? ```python
import json
with open('policy.json', 'r') as f:policy = json.load(f) ```
{
"rules": [
{"action": "allow", "src": "10.0.1.0/24"},
{"action": "deny", "src": "0.0.0.0/0"},
]
}A Python project has the following directory structure:
project/ __init__.py main.py subpackage/ __init__.py module.py
Inside 'module.py', there is a function 'func' that needs to be imported in 'main.py'. The team wants to import 'func' using a relative import from 'main.py'. However, when they run 'python main.py' from the project root, they get an ImportError. What is the most likely reason?
You are a Python developer working on a project with the following structure:
myapp/ __init__.py main.py modules/ __init__.py utils.py helpers.py
The file main.py contains:
from modules import utils from modules import helpers
utils.some_function() helpers.another_function()
When you run main.py, you get an ImportError: No module named 'modules'. However, the modules directory exists and both __init__.py files are present. The directory myapp is not installed as a package; you are running main.py directly from the myapp directory. What is the most likely cause and how should you fix it?
A developer creates a package 'mypackage' with the following structure:
mypackage/ __init__.py module1.py module2.py
The __init__.py contains:
from mypackage.module1 import func1 from mypackage.module2 import func2
__all__ = ['func1', 'func2']
In a separate script, the developer writes:
from mypackage import * print(func1())
This works as expected. However, when the developer runs the same script from a different directory (not the one containing mypackage), the import works but the script prints an error that func1 is not defined. What could be the problem?
Refer to the exhibit. ``` # package/__init__.py from . import submodule # package/submodule.py from . import __init__ # main.py import package ``` Error: ``` ImportError: cannot import name '__init__' from partially initialized module 'package' (most likely due to a circular import) ```
A team is developing a large application that consists of multiple components, each housed in separate directories. They want to organize these components under a common namespace package called 'app'. The directory structure is as follows:
/opt/project/ ├── components/ │ ├── auth/ (contains __init__.py and modules) │ └── billing/ (contains __init__.py and modules) ├── main.py └── another_location/ └── reports/ (contains __init__.py and modules)
The team wants to allow all these components to be imported as subpackages of 'app', e.g., 'import app.auth', 'import app.billing', 'import app.reports'. They have ensured that none of the directories named 'app' exist; instead, they plan to use namespace packages. They create a directory '/opt/project/app/' with an __init__.py file, and move the auth and billing directories under it. However, the reports directory must remain at '/opt/project/another_location/reports/', but they want it to be accessible as 'app.reports'. They attempt to achieve this by adding '/opt/project/another_location/' to the sys.path. When they run main.py, they get an ImportError: No module named 'app.reports'. The auth and billing modules work fine. What is the most likely issue and the correct fix?