Python modules
Python modules¶
A module is a python file that contains a collection of functions. Modules are used to to organize Python code with related functionality, which makes the code easier to understand and use. In addition to the modules that come with the installation of Python, contained in what is known as the Python Standard Library, there are other third-party modules which we get access to when we install other packages/libraries. We can also create our own modules.
To be able to use functionality from a module we need to import it. There are several different way of doing that:
import the entire module with
import module_namefor example,
import pandasin that case, to use the
DataFrameobject, we need to typepandas.DataFrame
import a module using a different name (alias) with
import module_name as chosen_aliasfor example
import pandas as pdin that case, to use the
DataFrameobject, we need to typepd.DataFramepd is a common alias for pandas.
Instead of importing the entire module as above, we can import specific functions or objects from it with
from module_name import object1, object2for example
from pandas import DataFrame, Seriesin that case, to use the
DataFrameobject, we just typeDataFrame