Concepts / Importing Specific Functions from Modules

Importing Specific Functions from Modules

An import statement tells Python to find a module file, execute its code, and make its contents available to your program.

  • Programming

An Import Starts a Process

An import statement is an instruction to Python to locate a module, load it, execute the code in that module, and make its contents available to your program. This is more than declaring a variable or defining a function: Python begins a search-and-execute process behind the scenes.

When you import a specific function, the purpose is to make that function available to the importing program rather than requiring the program to work with every module name at once.

The Import Sequence

requestsfindsloadsexposesimport statementSearch directoriessys.pathModule filefirst matching fileModule codeexecuteModule contentsavailable to the program
What happens after Python encounters an import statement, from locating the module to making its contents available?

The sequence has four important stages. Python first receives the import request. It then searches for the requested module. After finding a matching file or compiled version, it loads and executes the module's code. Finally, the module's contents become available to the importing program. Importing a specific function follows the same module-loading process; the difference is that the program asks for a particular function from the module's contents.

Tracing a Specific Function Import

Suppose a program requests one function from a module.

Request: The import statement identifies a module and a specific function that the program wants to use.

Locate: Python searches for the module using its module-search process.

Initialize: Python loads the module and executes the code in its body.

Make available: The requested function is made available to the importing program as part of the module's contents.

The function becomes available only after Python has located and initialized its module.

The Search Path

For a module that is not built into Python, Python searches directories listed in sys.path. The directories are checked in a specific order, and Python stops when it finds the first matching module file. This order determines which file is selected when more than one searchable directory could contain a matching name.

check firstif no matchif no matchstop at matchRequested moduleDirectory 1first sys.path entryDirectory 2next sys.path entryDirectory 3later sys.path entryFirst matchmodule file
How does Python use sys.path to locate the requested module?

You can think of sys.path as an ordered set of breadcrumbs. Python walks through the directories one at a time, looking for a file whose name matches the requested module. The directory containing the running script is typically checked first. Standard library directories and site-packages are also among the locations Python may search. The exact list can be inspected by printing sys.path after importing sys.

One-Time Initialization

Python executes a module's body only the first time that module is imported. After the module has been loaded, Python caches it in memory. Later imports of the same module reuse the cached module instead of executing its initialization code again.

importinitializereuseModule not loadedFirst importexecute module codeCached moduleloaded in memoryLater importreuse cached module
What changes after the first import so later imports reuse the module instead of executing its code again?

The important change is the module's status: before the first import, Python must locate and initialize it; after the first import, the initialized module is cached. A later import therefore returns the cached version without re-running the module's initialization code. This avoids the overhead of re-executing the module and re-creating its variables and functions.

containsmade availableModulecontains functionsSpecific functionrequested contentImporting programfunction available here
Where does the requested function appear in the importing program, and what does the imported name refer to?

Specific Function Imports

The specific-function form asks Python to make one named function from a module available to the importing program. The module still has to be located, loaded, and initialized first. Importing one function does not skip the module search or the module's initialization process; it identifies which part of the module's contents the program intends to use.

frommodule sourcemodulemodule to locateimportrequest contentsfunctionspecific content
How does the specific-function import form identify the module and the function to make available?

Separating the Two Names

Read a specific-function import as a request involving a module name and a function name.

Module name: The module name tells Python which module it must locate through its import process.

Function name: The function name identifies the particular function from that module that the importing program wants available.

Initialization: Python still executes the module's code the first time the module is imported.

Reuse: If the module has already been initialized, Python reuses the cached module instead of executing its code again.

A specific-function import narrows which module content the program requests, but it does not remove the module-location and initialization steps.

Mistakes to Avoid

  • Assuming an import only retrieves an already available object.

    Python may need to search for the module, load it, execute its code, and make its contents available.

    Fix: Trace the full process: search, load, execute, and expose the module contents.

  • Assuming Python searches every directory after finding a match.

    Python searches directories in order and stops at the first match.

    Fix: Check the order of sys.path when investigating which module Python may locate.

  • Expecting a module to reinitialize every time it is imported.

    The module is cached after its first import, and later imports reuse it without re-executing its code.

    Fix: Treat module initialization as a one-time process during the program's execution.

  • Ignoring naming conflicts.

    The search order in sys.path can cause Python to find the local file instead of the expected module.

    Fix: Use module names carefully and consider sys.path order when debugging unexpected imports.

When an import behaves unexpectedly, ask three questions: Which module name did the statement request? Which directories appear in sys.path and in what order? Has the module already been initialized and cached? These questions connect the visible import statement to Python's search, execution, and reuse behavior.

Trace the Process

MEDIUM

A program imports one function from a module, and later another statement imports the same module. Describe the sequence of events for the first import and explain what changes during the later import.

Hints
  • Start with the directories in sys.path.
  • Identify when the module's code runs.
  • Explain what Python reuses after the first import.
  1. An import statement triggers a search, load, execution, and availability process.
  2. Python searches directories in sys.path in order and stops at the first matching module.
  3. The first import executes the module's initialization code and caches the loaded module.
  4. Later imports reuse the cached module without re-executing its initialization code.
  5. Importing a specific function identifies one requested part of a module, but the module still must be located and initialized.

Key Takeaways

  • Importing is an active search-and-execute process, not merely a variable declaration.
  • sys.path controls the ordered directory search for non-built-in modules.
  • Python stops at the first matching module file it finds.
  • A module's code runs once on first import, then the cached module is reused.
  • A specific-function import requests one function from a module while still relying on the module's normal loading process.