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