The from keyword in Python is mainly used for importing specific parts of a module rather than the entire module. It helps in making the code cleaner and more efficient by allowing us to access only the required functions, classes, or variables.
Here's an example.
Python
# Importing only sqrt function of math modeule
from math import sqrt
print(sqrt(25))
In the above code, instead of importing the entire math module, we only import the sqrt function with the help of "from" keyword and use it directly using "sqrt()" without needing to write math.sqrt()
Syntax:
from module_name import specific_function_or_class
module_name : name of the module needed.
specific_function_or_class: name of the required specific function or class from the module.
Let's explore how to use "from" keyword with different examples.
To import a Specific Function/Class
Python
from math import sqrt
print(sqrt(2025))
To Import Multiple Functions/Classes
Instead of importing a single function or class from a module, we can import multiple function and classes with the help of "from" keyword. Here's how,
Python
from math import sqrt, ceil, floor
print(sqrt(2025))
print(ceil(7/2)) # 7/2 is 3.5
print(floor(7/2))
In the above code we have imported multiple functions from the math module and used them directly. It increases the flexibility and efficiency.
Importing Everything from a Module
Although it's not recommended to import everything from a module while coding, but in-case if we are required to do it, we can do it by using '*' operator and simple use any function or class required from that module.
Python
from math import *
print(sqrt(25))
Using from for Relative Imports In Packages
Relative imports are used inside packages to import modules from the same or different levels of the package hierarchy. It's done with the help of "from" keyword. Let's ubderstand with an example.
Suppose we have the following package structure:
mypackage/
│── __init__.py
│── module_a.py
│── module_b.py
│── subpackage/
│── __init__.py
│── module_c.py
Now, let's say module_b.py wants to import a function named "func" from module_a.py. Here's how we can do it:
Python
from .module_a import func
Explanation:
- Using "from", we can navigate through the package.
- .module_a: the single dot ( . ) means in the "current package", module_a.
- import func, imports the function - "func".
Similar Reads
Python def Keyword Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In Python, a function is a logical unit of code containing a sequence of statements indented under a name given using the âdefâ keyword. In Python def
6 min read
as Keyword - Python as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe
3 min read
Python in Keyword The in keyword in Python is a powerful operator used for membership testing and iteration. It helps determine whether an element exists within a given sequence, such as a list, tuple, string, set or dictionary.Example:Pythons = "Geeks for geeks" if "for" in s: print("found") else: print("not found")
3 min read
Python Keywords Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variables, functions, and classes or any other identifier. Getting List of all Python keywordsWe can also get all the keyword names usin
2 min read
python class keyword In Python, class keyword is used to create a class, which acts as a blueprint for creating objects. A class contains attributes and methods that define the characteristics of the objects created from it. This allows us to model real-world entities as objects with their own properties and behaviors.S
1 min read