FlashKeepers

Python · Python

Python Modules and Packages

Importing modules, organizing packages, and using pip to manage third party libraries, each explained with a short example.

29 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone
What is a module in Python?
A file containing Python code that can be imported and reused. File name becomes the module name.
How do you import a module named math?
import math
How do you call a function from the math module?
math.function_name(). Example: math.sqrt(16)
How do you import a specific function from a module?
from module_name import function_name. Example: from math import sqrt
How do you import with an alias?
import module_name as alias. Example: import numpy as np
What does 'from x import *' do?
Imports all public names from module x into the current namespace.
How do you see all attributes in a module?
Use dir(module_name). Example: dir(math)
What is a package in Python?
A directory containing Python modules and an __init__.py file. Allows nested organization of modules.
What is the purpose of __init__.py?
Makes Python treat a directory as a package. Can be empty or contain package initialization code.
How do you import a module from a subpackage?
from package.subpackage import module. Example: from os.path import join
What is the __name__ variable?
Set to '__main__' when script runs directly, otherwise set to module name when imported.
What does 'if __name__ == "__main__":' do?
Lets you write code that only runs when script executes directly, not when imported as a module.
What is a relative import in a package?
Import using dot notation for sibling or parent modules. Example: from . import sibling or from .. import parent_module
What is pip?
Package installer for Python. Command-line tool for installing and managing third-party packages.
How do you install a package with pip?
pip install package_name. Example: pip install requests

14 more cards in the app