FlashKeepers

Python · Python · College and University

Python Modules and Packages

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

About this set

29 basic cards for Python Python. It opens with "What is a module in Python?" and "How do you import a module named math?". More College and University decks are listed below.

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

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is a module in Python?
Show answerA file containing Python code that can be imported and reused. File name becomes the module name.
How do you import a module named math?
Show answerimport math
How do you call a function from the math module?
Show answermath.function_name(). Example: math.sqrt(16)
How do you import a specific function from a module?
Show answerfrom module_name import function_name. Example: from math import sqrt
How do you import with an alias?
Show answerimport module_name as alias. Example: import numpy as np
What does 'from x import *' do?
Show answerImports all public names from module x into the current namespace.
How do you see all attributes in a module?
Show answerUse dir(module_name). Example: dir(math)
What is a package in Python?
Show answerA directory containing Python modules and an __init__.py file. Allows nested organization of modules.
What is the purpose of __init__.py?
Show answerMakes Python treat a directory as a package. Can be empty or contain package initialization code.
How do you import a module from a subpackage?
Show answerfrom package.subpackage import module. Example: from os.path import join
What is the __name__ variable?
Show answerSet to '__main__' when script runs directly, otherwise set to module name when imported.
What does 'if __name__ == "__main__":' do?
Show answerLets you write code that only runs when script executes directly, not when imported as a module.
What is a relative import in a package?
Show answerImport using dot notation for sibling or parent modules. Example: from . import sibling or from .. import parent_module
What is pip?
Show answerPackage installer for Python. Command-line tool for installing and managing third-party packages.
How do you install a package with pip?
Show answerpip install package_name. Example: pip install requests

14 more cards in the app

Study this set free