You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

7.2 Importing names

3 min readjune 24, 2024

Python's system is a powerful feature that allows you to organize and reuse code efficiently. By importing specific functions or entire modules, you can access a wide range of pre-built functionality to enhance your programs.

Understanding how to modules and manage packages is crucial for writing clean, efficient Python code. This knowledge enables you to leverage the vast ecosystem of Python libraries, both built-in and third-party, to solve complex problems with ease.

Importing Modules and Functions in Python

Importing specific functions

Top images from around the web for Importing specific functions
Top images from around the web for Importing specific functions
  • Use
    [from](https://www.fiveableKeyTerm:From)
    keyword followed by module name and
    import
    to import specific functions from a module
    • Syntax:
      from module_name import function_name
    • from [math](https://www.fiveableKeyTerm:Math) import [sqrt](https://www.fiveableKeyTerm:sqrt)
      imports only
      sqrt
      function from
      math
      module
  • Allows using imported function directly without prefixing it with module name
    • After importing
      sqrt
      from
      math
      , use
      sqrt(4)
      instead of
      math.sqrt(4)
  • Import multiple functions from a module by separating them with commas
    • from math import sqrt, [sin](https://www.fiveableKeyTerm:sin), [cos](https://www.fiveableKeyTerm:cos)
      imports
      sqrt
      ,
      sin
      , and
      cos
      functions from
      math
      module

Prevention of name collisions

  • Name collisions occur when imported names conflict with existing names in code or other imported modules
  • Use
    as
    keyword to create an for imported module or function to avoid name collisions
    • Syntax:
      import module_name as alias
      or
      from module_name import function_name as alias
    • import [matplotlib.pyplot](https://www.fiveableKeyTerm:matplotlib.pyplot) as plt
      creates alias
      plt
      for
      matplotlib.pyplot
      module
  • Using aliases prevents name collisions and makes code more readable by providing shorter or more descriptive name
  • Another way to prevent name collisions is to import entire module and use module name as prefix when accessing its functions or variables
    • import math
      and then use
      math.sqrt(4)
      to avoid conflicts with other
      sqrt
      functions

Specific vs entire module imports

  • Importing specific functions:
    • Advantages:
      • Use imported functions directly without prefixing with module name, making code more concise
      • Reduces risk of name collisions by only importing required functions
      • Can make code more readable by explicitly showing which functions are used from a module
    • Disadvantages:
      • Requires knowing exact names of functions to import
      • May result in longer import statements if importing many functions from a module
  • Importing entire modules:
    • Advantages:
      • Access all functions and variables from module using module name as prefix
      • Requires less knowledge of specific function names within module
      • Can make shorter if using many functions or variables from module
    • Disadvantages:
      • Increases risk of name collisions if module contains names that conflict with code or other imported modules
      • May make code less readable if module name is long or not descriptive enough
      • Can potentially import unnecessary functions or variables not used in code

Package Management and Libraries

  • Python's provides built-in modules for common tasks
  • Third-party libraries extend Python's functionality beyond the standard library
  • Use (Python installer) to install and manage third-party libraries
  • Packages are collections of modules organized in directories
    • The file in a directory indicates it should be treated as a package
  • Relative imports allow importing modules within the same package using dot notation
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary