What is a package in Python?
A. A single file containing variables
B. A module with only classes
C. A directory containing a collection of related multiple modules along with a special __init__.py file.
D. A zip file with Python files
A directory containing a collection of related multiple modules along with a special __init__.py file.
In Python, a package is a directory containing a special __init__.py file and can include multiple modules and sub-packages. This structure allows hierarchical module organization.
What is the main purpose of __init__.py in a package?
A. It defines the main class in the package.
B. It signals to Python interpreter that the directory is a package.
C. It contains package metadata only.
D. It compiles all modules into one
It signals to Python interpreter that the directory is a package.
The presence of __init__.py tells Python that the directory should be treated as a package. You can also use it to initialize package-level data or expose selected modules.
How do you import a module from a package in Python?
A. import module.package
B. from module.package import*
C. include package.module
D. import package.module
import package.module
The correct syntax to import a module from a package is import package.module. This accesses the module inside the package namespace.
What happens if __init__.py is missing in a directory?
A. The package won’t be detected by Python 3.x.
B. Python treats it as a namespace package in Python 3.3+.
C. An error is raised
D. Only sub-packages work
Python treats it as a namespace package in Python 3.3+.
In Python 3.3+, a directory without __init__.py is treated as a namespace package implicitly.
How will you import a sub-package module in Python?
A. import package.subpackage.module
B. import module.subpackage.package
C. from subpackage import module
D. import subpackage.module
import package.subpackage.module
You use dot notation to access nested modules/sub-packages like package.subpackage.module.
Which of the following function allows dynamic import of a package at runtime?
A. __import__()
B. open()
C. importlib.init()
D. exec()
__import__()
__import__() is a built-in function that allows dynamic importing of modules or packages.
Which command is used to install external packages (or third-party packages) in Python?
A. pip install package_name
B. python install package_name
C. install package_name
D. import package_name
pip install package_name
pip install package_name is the standard command to install Python packages from PyPI.
How can you check all installed packages in your Python environment?
A. python –list-packages
B. pip show
C. pip list
D. None of the above
pip list
pip list displays all installed packages in your current environment.
What is a namespace package?
A. A package with no sub-modules.
B. A package not listed in __all__.
C. A package without an __init__.py file.
D. A package in a ZIP file.
A package without an __init__.py file.
Namespace packages are packages without __init__.py files that allow splitting a single package across multiple directories.
How to import a class from a submodule of a package?
A. import package.classname
B. from package.submodule import ClassName
C. include package.submodule.ClassName
D. using package.ClassName
from package.submodule import ClassName
- To import a class from a submodule, use from package.submodule import ClassName.
- This is the standard way to import a specific class from a submodule within a package in Python.
- In the above syntax, package defines the root package name.
- The submodule is the module (Python file) inside the package, where the class is defined.
- ClassName is the specific class you want to import.
Which file is essential to make a folder installable via pip?
A. __init__.py
B. requirements.txt
C. LICENSE.txt
D. setup.py
setup.py
The setup.py file is used to configure package installation and metadata. It contains metadata like name, version, and dependencies for packaging.
Which function returns the file path of a package module?
A. __file__
B. __path__
C. pkgutil.path()
D. os.path()
__file__
The __file__ gives the path to the current module or package file.
Which directory structure is ideal for Python packages?
A. All files in one folder
B. Scripts > Package > Module
C. Package > Module > Scripts
D. Lib > Bin > Package
Package > Module > Scripts
The ideal Python package structure follows a hierarchical organization that separates package (top-level directory with __init__.py), modules (.py files containing functions, classes, and variables), and scripts (executable files, often in a scripts/ or bin/ directory).
How to create a virtual environment in Python 3?
A. python3 -m venv env_name
B. python3 -m virtual
C. env python create
D. python -venv
python3 -m venv env_name
- The correct syntax is python3 -m venv env_name to create an isolated environment.
- In this syntax, python3 -m venv creates a virtual environment using Python’s built-in venv module.
- env_name is the name of the directory where the virtual environment will be stored.
Which of the following is the function of __all__ in a package’s __init__.py file?
A. To list all the modules that should be imported when from package import * is used.
B. To specify all the functions in the package.
C. To document all package dependencies.
D. To list all Python files in the package.
To list all the modules that should be imported when from package import * is used.
__all__ is a list of module names that should be imported when you use from package import *.