Open In App

External Modules in Python

Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external modules. In this article, we will learn about the external modules in Python and how to use them.

What are External Modules?

External modules are collections of pre-written code that offer additional functions, classes, or methods not available in Python’s standard library. These modules usually cater to specific needs like web scraping, data analysis, or machine learning. Python’s package manager, “pip”, makes it exceedingly simple to install and manage these modules.



Advantages of Using External Modules

List of Most Popular Python External Modules

Here’s a list of some of the most popular external Python modules across various domains. Below are just a few of the most popular external modules available in the vast collection of Python packages. Depending on the domain and specific needs, there are countless other valuable packages available to Python developers.

1. Web Development:



2. Data Analysis and Manipulation:

3. Machine Learning and Artificial Intelligence:

4. Data Visualization:

5. Web Scraping:

6. Networking and APIs:

7. Databases:

8. Testing:

9. GUI Development:

10. Scientific Computing:

Examples of External Modules

Let’s understand it by using code examples of two popular external Python modules.

Example 1: Using “requests” module in Python

The “requests” module simplifies the process of making HTTP requests in Python. We can install this module by executing the below command in terminal.

pip install requests

With just a few lines, we are able fetch, send, and handle HTTP requests and responses.




# Import request module
import requests
 
# store the response from an API in response
response = requests.get("https://api.github.com")
 
# Checking if the reponse status
if response.status_code == 200:
    print("Successfully connected to GitHub API!")
else:
    print("Failed to connect to GitHub API!")

Output:

Example 2: Using “pandas” module in Python

“pandas” is a powerful data manipulation and analysis tool for Python. Using “pandas”, we can handle vast datasets, manipulate data structures, and perform complex data operations with ease. To install “pandas” module execute the below command in terminal.

pip install pandas

Below is a simple example showcasing reading a CSV file and displaying its first five rows:




# Import pandas module
import pandas as pd
 
# Read a CSV file
dataframe = pd.read_csv('datafile.csv')
 
# Display the first five rows
print(dataframe.head())

Ouput:

Example 3: Using “numpy” module in Python

Numpy library is used for numerical computing in Python. It provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. We can install this library by executing below command in our terminal.

pip install numpy

In the below code, we first import the numpy library and then create an one dimensional array using array() function after that we print the mean of all the elements in an array by using mean() function inside the print() function.




# Import numpy library
import numpy as np
 
# Create an array
arr = np.array([1, 2, 3, 4, 5,
               6, 7, 8, 9, 10])
 
# Calculate the mean
print(np.mean(arr))

Output:

Example 4: Using “matplotlib” module in Python

Matplotlib is a plotting library that is used to visualize large datasets. It provides an object-oriented API to embed plots into applications using general-purpose GUI toolkits. To install “matplotlib” execute the below command in the terminal.

pip install matplotlib

In the below example, we draw a simple plot by using the sample data set.




# Import matplotlib module
import matplotlib.pyplot as plt
 
# Create a sample Data
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
 
# Create a plot
plt.plot(x, y)
 
# Show the plot
plt.show()

Output:

Example 5: Using “flask” module in Python

Flask is a lightweight framework for web development in Python. It’s easy to use and allows for the rapid development of web applications. We can install flask by executing the below command in terminal.

pip install flask

In the below example, we have written the script to display the message “Hello, Flask!” on the webpage.




# import Flask from flask module
from flask import Flask
 
# Creating an instance of flask app
app = Flask(__name__)
 
# Defining a route
@app.route('/home')
def home():
    return "Hello, Flask!"
 
# Run the application
if __name__ == '__main__':
    app.run()

Output:

These are only the few examples of Python external modules. There are lot more external module available to perform various tasks according to our requirements. Whether we’re developing a web application, analyzing data, or venturing into artificial intelligence, there’s likely an external module available to make our tasks easy.


Article Tags :