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_name
for example,
import pandas
in that case, to use the
DataFrame
object, we need to typepandas.DataFrame
import a module using a different name (alias) with
import module_name as chosen_alias
for example
import pandas as pd
in that case, to use the
DataFrame
object, we need to typepd.DataFrame
pd 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, object2
for example
from pandas import DataFrame, Series
in that case, to use the
DataFrame
object, we just typeDataFrame