Python import Statements: Modules and Libraries

You do not have to hard-code everything you need to implement your projects. You only have to import the functions, variables, and classes built for these tasks.

Import statements eliminate the need to hard-code solutions to common programming problems. This keeps your code well structured, easy to maintain, and saves time.

python import statements

What is a Module?

A module is a file or document that contains some python code (.py extension).

You can store variables, functions, and classes in a python file and import them whenever you need them. That way, you do not have to write the same blocks of code severally in your project.

Moreover, any changes made to the module would be effected across all documents from which it has been imported.

Python Built-in Modules

Every python installation is equipped with modules that are available to every python program.

These modules consist of variables, functions, and classes which can be imported into any python program. They are contained in the Python Standard Library.

One of these modules is the math module. Just as the name implies, it contains mathematical functions.

Let’s run the help function to inspect the module.

python help function

As the description states, this module provides access to mathematical functions.

Let’s try out some of these functions.

python built-in functions

The dot syntax was used in accessing these functions. math.pow() simply tells python to access the pow function in the math module. This method is advisable whenever you need to use different functions with the same name. It avoids conflict among these functions.

Importing Specific Functions

Importing a whole module would consume more computation space and time than importing specific functions from the module.

You can import just the functions you’ll be needing for your program to save up space and time.

Let’s say you only need the radians, sin, cos, and tan functions from the math module.

python specific imports

You can also import all the functions from a module without having to access them with the dot syntax.

python import

Python Libraries

A library is simply a collection of modules.

Each python installation comes with a standard library that contains modules like the math module discussed earlier.

Third-Party Libraries

These libraries don’t come preinstalled with your python installation. You would have to use package installers like pip to install the libraries you need.

You do not need to install pip manually as it comes preinstalled with Python version 3.0 and above.

However, If you need to install pip, check out the installation guide here

Now that you have pip, you can proceed to install any package of your choice from PyPi.

PyPi is an official third-party software repository for Python.  

Scikit-learn

This is a third-party library in python used for building machine learning models.

Anaconda distribution is equipped with many data science libraries including sci-kit-learn.

However, if you need to pip install sci-kit-learn on your PC, follow this guide.

Working with Libraries

There are numerous classes and functions for various machine learning algorithms in the sci-kit-learn library.

For example, the LinearRegression class exists in the linear_model package.

We can access the LinearRegression class using the dot syntax or import it specifically.

from sklearn import linear_model

regressor = linear_model.LinearRegression()

The first line tells Python to import the linear_model package from the sklearn library.

The second line accesses the package to build a linear regression model.

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

The first line tells Python to look into the linear_model package in sklearn and import only the LinearRegression class.

The second line builds a linear regression model without the dot syntax.

The first method allows other classes and functions to be accessible by our python program consumes more computation space and time.

The second method allows only the LinearRegression class to be accessible by our class. This method is recommended if you would be needing just a few classes for your program.

We'll continue working with libraries in the next article.

Summary

Import statements are a great way to eliminate writing solutions to common programming problems from scratch. They make your work clean, less stressful, and easy to maintain.

No comments:

Powered by Blogger.