Logo

dev-resources.site

for different kinds of informations.

Python: Imports and modules

Published at
8/9/2023
Categories
python
modules
programming
beginners
Author
nimo08
Author
6 person written this
nimo08
open
Python: Imports and modules

When writing scripts in Python, some definitions prove to be useful in different scripts; instead of rewriting these definitions in several files, importing modules can make this process less cumbersome.

What is a module?

  • A module is a file containing Python definitions and statements, the specific file name is simply suffixed by .py
  • Importing a module in Python allows you to access the functions, classes, variables, and other objects defined within that module for use within your Python script.

What are the contents of a module?

  • Python modules contain various objects such as functions, variables among others.
  • For example: we have module named calc; within the calc.py file we have 4 function definitions for mathematical operations i.e., add, sub, multiply, divide.
  • We can import the calc module into a script where we want to perform these mathematical operations instead of redefining them again within the script.
  • Modules contain executable statements as well, which are used to initialize the module and are run only the first time the module name is encountered in an import statement.

How to import modules

  1. import 'module': this allows access to the entire module from the script.
  2. from 'module' import 'object': using the β€˜from’ keyword allows you to access specific objects within the module i.e. from math import pi; this imports the pi variable of the math module.
  3. from 'module' import *: using the * wildcard allows access to all the names within the module except those starting with an β€˜_’; this is not recommended as it introduces an unknown set of names into the interpreter, possibly concealing some parts that you have already defined.
  4. python3 -m 'module' 'file_name': modules can be imported in the command-line as well; the -m represents the module, we then specify the module name, and the name of the file in which we want to use the module.

Module search path

  • When a module is imported e.g. calc, the interpreter first searches for a built in module with the name calc, the module names are listed in sys.builtin_module_names; if the name is not found, it proceeds to search for the calc.py in a list of directories given by the variable sys.path.

References:

modules Article's
30 articles in total
Favicon
Python Day-9 Predefined modules,While loop,Task
Favicon
Understanding JavaScript Modules: Exporting and Importing Code Made Easy
Favicon
Python Day - 8 Modules-Meaning and Types,Tasks
Favicon
Day 8 - Modules
Favicon
Node Express
Favicon
Terraform Modules Best Practices for Better Cloud Management
Favicon
Exploring Medusa 2.0 Commerce Modules
Favicon
Advanced Terraform Module Usage: Versioning, Nesting, and Reuse Across Environments
Favicon
Modules 1/2
Favicon
Collaborate and Stay Ahead with NEW Forge Updates
Favicon
Mind the Terraform Modules
Favicon
The Ongoing War Between CJS & ESM: A Tale of Two Module Systems
Favicon
Top 10 AI Edge Engineer Modules by Microsoft
Favicon
Meet the new Puppet Forge
Favicon
How to reduce the cost of LED display modules
Favicon
Terraform - Modules & Demos
Favicon
Types of Node Modules
Favicon
Unraveling the Monolith: Terraform Configuration Refactoring in Depth
Favicon
πŸ“Œ How can you use Terraform modules?
Favicon
EcmaScript Modules na prΓ‘tica
Favicon
The Go Playground: Using Modules
Favicon
What is the general resolution of LED display modules?
Favicon
What are the main components of an LED display?
Favicon
Python: Imports and modules
Favicon
Golang Environment: GOPATH vs go.mod
Favicon
Javascript Modules
Favicon
Install my development environment In Prestashop
Favicon
Reversing a Linked List: A Hallway Analogy
Favicon
Everything You Need To Know About Node.js Modules
Favicon
Content & Tooling Team Status Update

Featured ones: