How to remove unused modules in a Python script ?

Published: February 05, 2024

Tags: Python;

DMCA.com Protection Status

Introduction

I have created multiple Jupyter notebooks for my work, and when I start a new one, I often find myself copying and pasting previous work. I admit, it's partly because I'm lazy. However, in doing so, I sometimes end up with unused modules that I defined at the beginning. So, I started searching for a quick way to identify these unused modules in a Python script and how to clean up my code before sharing it with others.

Here's how you can easily remove any modules that are not being used:

Remove unused modules using autoflake

One way to identify unused modules in a Python script is by utilizing autoflake. To install autoflake, simply execute the following command:

pip install --upgrade autoflake

Once installed, you can apply autoflake to any of your Python scripts. For instance, you can use the command below to remove all unused imports from a script named "myscript.py":

autoflake --remove-all-unused-imports myscript.py

For example, when using my script, it returns:

--- original/Untitled10.py
+++ fixed/Untitled10.py
@@ -8,32 +8,16 @@
 import numpy as np
 import numpy.ma as ma
 import pandas as pd
-import glob
 import matplotlib.pyplot as plt
 import matplotlib.cm as cm
 import matplotlib
 import warnings
-import pyproj
 import matplotlib as mpl
-import cartopy.crs as ccrs
-import matplotlib.patches as mpatches
-import re
-import random
-import datetime
 import geopandas
-import htmlmin
 import warnings

-from bokeh.plotting import figure, show, save
+from bokeh.plotting import figure, show
 from bokeh.plotting import output_notebook
-from bokeh.plotting import output_file, save
-from bokeh.models.tools import PanTool, SaveTool, WheelZoomTool, BoxZoomTool, ResetTool
-from sklearn.cluster import AgglomerativeClustering
-from datetime import datetime, timedelta
-from scipy.spatial.distance import cdist
-from scipy.interpolate import griddata
-from scipy.interpolate.interpnd import _ndim_coords_from_arrays
-from scipy.spatial import cKDTree
 from scipy import stats

Lines starting with a "-" indicate unused modules.

Remove unused modules in a jupyter notebook

To remove unused modules in a Jupyter notebook, we can simply start by converting it into a Python script. There are two options available for this task. The first option is to navigate to the Jupyter notebook, click on "File," then select "Download as," and finally choose "Python" from the dropdown menu (as shown in the screenshot below).

How to remove unused modules in a Python script ?
How to remove unused modules in a Python script ?

Alternatively, we can use the following command:

jupyter nbconvert --to python filename.ipynb

Substitute the term "filename" with the name of your Jupyter notebook.

Subsequently, we can employ autoflake as we did in the preceding section.

autoflake --remove-all-unused-imports filename.py

References

Image

of