0% found this document useful (0 votes)
50 views5 pages

Modules and Packages 50 QA

Uploaded by

dubeykaran2356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Modules and Packages 50 QA

Uploaded by

dubeykaran2356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Modules & Packages in Python: import, custom

modules, virtual environments

Q1. What is a module in Python?


Ans: A module is a file containing Python definitions, functions, and variables that can be reused in
other programs.

Q2. How do you create a module in Python?


Ans: Simply save Python code in a `.py` file. For example, `mymodule.py` can be imported using
`import mymodule`.

Q3. How do you import a module in Python?


Ans: Use the `import` statement. Example: `import math` imports the math module.

Q4. What is the difference between `import module` and `from


module import`?
Ans: `import module` requires prefix (e.g., math.sqrt()), while `from module import sqrt` allows direct
use (sqrt()).

Q5. How do you give an alias to a module?


Ans: Use the `as` keyword. Example: `import numpy as np`.

Q6. What are built-in modules in Python?


Ans: Modules that come pre-installed with Python, like `math`, `os`, `sys`, `random`.

Q7. How do you check all functions inside a module?


Ans: Use the `dir(module_name)` function.

Q8. What is the purpose of the `__name__` variable in a module?


Ans: It helps differentiate if the module is being run directly or imported into another file.

Q9. What is a package in Python?


Ans: A package is a collection of modules organized in directories with an `__init__.py` file.

Q10. What is the difference between a module and a package?


Ans: A module is a single `.py` file, while a package is a collection of modules in a directory.

Q11. What is the role of `__init__.py`?


Ans: It marks a directory as a Python package and can execute initialization code when imported.

Q12. How do you import a function from a module?


Ans: Use: `from module import function`. Example: `from math import sqrt`.

Q13. What happens if two modules have the same function name?
Ans: The last imported module overrides the earlier one if imported directly.

Q14. How do you import multiple modules in one line?


Ans: Example: `import os, sys, math`.

Q15. What is the `sys.path` list used for?


Ans: It contains directories where Python looks for modules to import.

Q16. How can you reload a module in Python?


Ans: Use `importlib.reload(module_name)`.

Q17. What is a custom module?


Ans: A custom module is a `.py` file created by the user containing functions, classes, and
variables.

Q18. How do you import a custom module?


Ans: Save the file in the same directory or add its path, then use `import module_name`.

Q19. How do you import a module from a different directory?


Ans: Add the directory path to `sys.path` or use a package structure.

Q20. What is a namespace in Python modules?


Ans: A namespace is a mapping between names and objects; modules create their own
namespace.

Q21. What is the purpose of `import *`?


Ans: It imports all public functions, variables, and classes from a module.
Q22. Why is `import *` discouraged?
Ans: It pollutes the namespace and can overwrite existing variables/functions.

Q23. How do you install external Python packages?


Ans: Use `pip install package_name`.

Q24. How do you list installed packages in Python?


Ans: Run `pip list` in the terminal.

Q25. What is a virtual environment in Python?


Ans: A virtual environment is an isolated environment that allows you to install packages without
affecting global Python.

Q26. How do you create a virtual environment?


Ans: Run `python -m venv env_name`.

Q27. How do you activate a virtual environment?


Ans: On Windows: `env_name\Scripts\activate`. On macOS/Linux: `source env_name/bin/activate`.

Q28. How do you deactivate a virtual environment?


Ans: Simply run `deactivate`.

Q29. Why are virtual environments important?


Ans: They prevent dependency conflicts between different projects.

Q30. What is `pip freeze` used for?


Ans: It lists installed packages with versions, useful for creating requirements.txt.

Q31. How do you install packages from requirements.txt?


Ans: Run `pip install -r requirements.txt`.

Q32. What is the difference between `pip` and `conda`?


Ans: `pip` is Python's package installer, while `conda` manages environments and packages
(supports multiple languages).
Q33. How do you uninstall a package in Python?
Ans: Run `pip uninstall package_name`.

Q34. How do you check the location of a module?


Ans: Use `module.__file__`.

Q35. What is a third-party module?


Ans: Modules developed by the community and installed via pip, e.g., requests, pandas, flask.

Q36. What is a standard library in Python?


Ans: The collection of built-in modules that comes pre-installed with Python.

Q37. What is `importlib` used for?


Ans: It provides functions to import and reload modules programmatically.

Q38. How do you check version of a package?


Ans: Run `pip show package_name` or `package.__version__` (if supported).

Q39. What is relative import in Python?


Ans: Importing modules from the same package using `.` or `..`. Example: `from . import module1`.

Q40. What is absolute import in Python?


Ans: Specifying the full path of the module from the project root. Example: `from mypackage import
module1`.

Q41. What happens if Python cannot find a module?


Ans: It raises a `ModuleNotFoundError`.

Q42. How do you avoid circular imports?


Ans: Restructure code, use local imports inside functions, or redesign module dependencies.

Q43. What is `sys.modules`?


Ans: A dictionary that caches all imported modules in a Python session.

Q44. What are namespace packages?


Ans: Packages without `__init__.py` that allow modules to be spread across multiple directories.

Q45. What is the role of `pkgutil`?


Ans: It provides utilities for working with packages and namespace packages.

Q46. What is the difference between `venv` and `virtualenv`?


Ans: `venv` is built-in from Python 3.3+, while `virtualenv` is an external tool with more features.

Q47. What is `pipenv`?


Ans: A tool that combines package management and virtual environments, simplifying dependency
management.

Q48. What is `pyenv`?


Ans: A tool for managing multiple Python versions and virtual environments.

Q49. Can we have nested packages in Python?


Ans: Yes, packages can contain sub-packages with their own modules.

Q50. How do you distribute a Python package?


Ans: By creating `setup.py` and uploading to PyPI using `twine`.

Q51. What is `__all__` in a module?


Ans: A list that defines which functions/classes are exported when `from module import *` is used.

You might also like