GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Modules - GeeksforGeeks Python ModulesLast Updated : 19 Sep, 2026 Module is a file containing definitions and statements. A module can define functions, classes and variables. Modules help organize code into separate files so that programs become easier to maintain and reuse. Instead of writing everything in one place, related functionality can be grouped into its own module and imported whenever needed. Create a ModuleTo create a module, write the desired code and save that in a file with .py extension. Example: Let's create a calc.py in which we define two functions, one add and another subtract.
This is all that is required to create a module. Import moduleModules can be used in another file using the import statement. When Python sees an import, it loads the module if it exists in the interpreter’s search path. Below is the syntax to import a module:
Example: Here, we are importing the calc that we created earlier to perform add operation.
Output
Explanation: import calc loads the module and calc.add() accesses a function through dot notation. Types of Import Statements1. Import From Module: This allows importing specific functions, classes, or variables rather than the whole module.
Output 4.0 720 Explanation: Only sqrt and factorial are brought into the local namespace, so the prefix math. is not required. 2. Import All Names: * imports everything from a module into the current namespace.
Output 4.0 720 Explanation: Every public name of math becomes directly accessible. (Not recommended in large projects due to namespace conflicts.) 3. Import With Alias: You can shorten a module's name using as.
Output 3.141592653589793 Explanation: math is accessed through the shorter alias m. Types of ModulesPython provides several kinds of modules. Each type plays a different role in application development. 1. Built-in Modules: These come bundled with Python and require no installation - e.g., math, random, os.
Output 4 Explanation: random.randint() returns a random number within the given range. 2. User-Defined Modules: These are modules you create yourself, such as calc.py.
Output
Explanation: The module is created manually and then imported into another script. 3. External (Third-Party) Modules: These modules are installed using pip - e.g., NumPy, Pandas, Requests.
Output
Explanation: requests is installed separately (pip install requests) and provides HTTP utilities. 4. Package Modules: A package is a directory containing multiple modules, usually with an __init__.py file. Example Directory
Using a module from a package
calls a function named some_func(), the output will be whatever that function returns. If utils.py contains something like:
Output
Locating a ModulePython searches for modules in a predefined list of directories known as the module search path. You can view this list using sys.path.
Output /home/guest/sandbox /usr/local/lib/python313.zip /usr/local/lib/python3.13 /usr/local/lib/python3.13/lib-dynload /usr/local/lib/python3.13/site-packages Explanation: Python checks each path in order until it finds the module you’re trying to import. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |