Modules and Packages in Python: Fundamentals for Data Scientists

Understand the basics with a concrete example!

Photo by Matthew Fournier on Unsplash
""" cvs_get_module.py
This module displays the summary of the tabular data contained in a CSV file
"""
import pandas as pdprint("cvs_get_module is loaded")def display_file_location(path, file_name):
print("File Location: {}".format(path+filename))
class CSVGetInfo:
def __init__(self, path, file_name):
self.path = path
self.file_name = file_name

def display_summary(self):
data = pd.read_csv(self.path + self.file_name)
print(self.file_name)
print(data.info())
Photo by Roger Bradshaw on Unsplash

Importing Modules

# ModulesExample.py
# Importing 'csv_get_module' and accessing its elements
import csv_get_moduledata_by_genres = csv_get_module.CSVGetInfo("/Users/erdemisbilen/
Lessons/", "data_by_genres.csv")
csv_get_module.display_file_location(data_by_genres.path, data_by_genres.file_name)data_by_genres.display_summary()Output:
cvs_get_module is loaded
File Location: /Users/erdemisbilen/Lessons/data_by_genres.csv
data_by_genres.csv
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2617 entries, 0 to 2616
Data columns (total 14 columns):

Importing the Module with Renaming

# ModulesExample.py
# Importing 'csv_get_module' and accessing its elements
import csv_get_module as cgdata_by_genres = cg.CSVGetInfo("/Users/erdemisbilen/Lessons/", "data_by_genres.csv")cg.display_file_location(data_by_genres.path, data_by_genres.file_name)data_by_genres.display_summary()Output:
cvs_get_module is loaded
File Location: /Users/erdemisbilen/Lessons/data_by_genres.csv
data_by_genres.csv
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2617 entries, 0 to 2616
Data columns (total 14 columns):

Importing Specific Names From a Module

from csv_get_module import display_file_location as dfldfl("/User/Python/","ModulesExample.py")
Output:
File Location: /UserModulesExample.py

Importing All Names Inside a Module

from csv_get_module import *

Hiding Elements of a Module

Photo by Tony Stoddard on Unsplash

Distinguishing Standalone Script Run from Module Load

""" cvs_get_module.py
This module displays the summary of the tabular data contained in a CSV file
"""
import pandas as pdprint("cvs_get_module is loaded")def display_file_location(path, file_name):
print("File Location: {}".format(path+filename))
class CSVGetInfo:
def __init__(self, path, file_name):
self.path = path
self.file_name = file_name

def display_summary(self):
data = pd.read_csv(self.path + self.file_name)
print(self.file_name)
print(data.info())
if __name__ == '__main__':data_by_genres = CSVGetInfo("/Users/erdemisbilen/Lessons/", "data_by_genres.csv")display_file_location(data_by_genres.path, data_by_genres.file_name)
data_by_genres.display_summary()
Photo by Tom Rogers on Unsplash

Packages In Python

Importing Modules from a Package

from utils.csv_get_module import display_file_location as dfldfl("/User/Python/","ModulesExample.py")Output:
File Location: /UserModulesExample.py

Key Takeaways

Conclusion

Machine Learning and Data Science Enthusiasts, Automotive Engineer, Mechanical Engineer, https://www.linkedin.com/in/erdem-isbilen/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store