“from” Keyword in Python: Importing Modules and Packages

"from" Keyword in Python: Importing Modules and Packages

In the world of programming, entering or pasting the entire code for even a simple program, activity or function is neither practical nor efficient. Most programming languages allow for modularity, often in the form of libraries, modules, and packages that comprise related functionalities or tasks. These libraries and files are imported at a specific part of the code directly into the program or file that needs them. The Python programming language is no exception, as it grants developers access to a broad range of such libraries by using an import keyword or the from keyword. In this tutorial, we will explore the functionality of the from keyword in Python as we dive into what it is, its syntax, and its usage via a series of examples.

The from Keyword in Python

The from keyword is among the most widely used commands in the Python programming language for accessing the functionality of Python modules and packages. We can use it to import specific functions, objects, classes, variables or modules that seem useful to us. Suppose we intend to access a specific object, i.e., a class, a function or even a module within another module or library, and we don’t want to import the entire library or module into our code; then, the from keyword is our go-to solution. This keyword is often accompanied by the import keyword when using it to import essential data or code from libraries and modules into our Python programs.

Syntax and Usage of from

Syntax of from

The from keyword’s syntax is the same across all Python versions, with no significant differences to note, whether it’s in Python 2 or Python 3. Its syntax is as follows:

from package_name import module_name

Or

From module_name import functionname

Example of Usage

The from keyword syntax may sound somewhat abstract and challenging to comprehend at first, but once we understand how to utilize it in practice, the code becomes increasingly apparent. In the following example, we will import the randrange() function from the random module in Python.

from random import randrange
print(randrange(0,15))

The above code imports the randrange() function from the random module in Python. We can then use the randrange() function to generate a random number using integers within the range of 0 to 15.

Here is an example output of the above code:

7

from with Renaming

Suppose we are importing code or data from a large library or module that may soon become unmaintainable and unwieldy. In that case, we may want to rename the imported module using the as keyword to make the code clearer and more understandable. This technique of renaming an imported module is known as aliasing.

Here is an example of aliasing that can be applied in conjunction with from:

from random import randrange as rr

print(rr(0, 10))

In this example, the randrange() function from the random module in Python has been an imported alias by using the as keyword. Our new module is now known as rr, which is significantly shorter and easier to read and understand than the longer module name.

Here is an example output of the above code:

6

from with Wildcard Character

Another practical feature that Python provides users when using the from keyword when importing modules is the ability to use wildcard characters. This option allows us to import an unspecified number of packages, objects, classes or even functions from within the libraries or modules.

The following example includes the * wildcard character in importing all objects, classes, and functions from within a module named example.

from example import *

The above code imports all objects, classes and functions from the example module into our current Python file or programming environment. However, it’s generally recommended to avoid using wildcards when importing code from other modules because it may lead to naming collisions and unexpected results or behavior within our code.

Conclusion

With this tutorial, we can see effectively how to utilize and syntax for the from keyword in Python. By using this essential tool, we can import specific modules, classes, functions, or objects that we need, allowing us to write cleaner and more efficient code.

We learned that we are not limited to importing code to our Python program in the standard import module manner we’ve been using but can also use the import from syntax to import specific functions, modules, classes or objects, even to select the names with a renaming option using the aliasing feature.

We hope that this guide has helped you to learn and understand the uses of the from keyword in Python and that you are now better equipped to utilize Python’s vast library of existing functionality to its fullest potential.

Leave a Reply

Your email address will not be published. Required fields are marked *