The Wayback Machine - https://web.archive.org/web/20240525002954/https://www.geeksforgeeks.org/external-modules-in-python/
Open In App

External Modules in Python

Last Updated : 27 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Efficiency: Instead of writing code from scratch, you can leverage pre-built functionalities.
  • Community Support: Many popular modules have vast communities, ensuring updates, bug fixes, and support.
  • Standardization: Using common modules means adhering to widely accepted best practices.

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:

  • Django: A high-level web framework that encourages rapid development and clean, pragmatic design.
  • Flask: A lightweight and flexible micro web framework.

2. Data Analysis and Manipulation:

  • pandas: Provides high-performance, easy-to-use data structures and data analysis tools.
  • numpy: Fundamental package for numerical computations in Python.

3. Machine Learning and Artificial Intelligence:

  • scikit-learn: Machine learning library featuring various algorithms and tools.
  • TensorFlow: Open-source machine learning framework developed by Google.
  • PyTorch: An open-source deep learning platform by Facebook.

4. Data Visualization:

  • matplotlib: Comprehensive library for creating static, animated, and interactive visualizations.
  • seaborn: Data visualization library based on matplotlib; provides a higher-level interface for drawing attractive and informative statistical graphics.

5. Web Scraping:

  • BeautifulSoup: Library for pulling data out of HTML and XML documents.
  • Scrapy: An open-source and collaborative web crawling framework.

6. Networking and APIs:

  • requests: Simplifies making HTTP requests.
  • socket: Low-level networking interface.

7. Databases:

  • SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) library.
  • sqlite3: Interface for SQLite databases.

8. Testing:

  • pytest: A robust framework for writing and executing tests.
  • unittest: (Part of the standard library) Unit testing framework.

9. GUI Development:

  • tkinter: Standard GUI library for Python.
  • PyQt: Set of Python bindings for the Qt application framework.

10. Scientific Computing:

  • SciPy: Library used for high-level computations.
  • SymPy: Python library for symbolic mathematics.

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.

Python3




# 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:

Screenshot-2023-10-17-115735

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:

Python3




# 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:
Screenshot-2023-10-17-124842

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.

Python3




# 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:

Screenshot-2023-10-17-132055

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.

Python3




# 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:

Screenshot-2023-10-17-132411

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.

Python3




# 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:

Screenshot-2023-10-17-140720

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.



Similar Reads

Reloading modules in Python
The reload() is a previously imported module. If you've altered the module source file using an outside editor and want to test the updated version without leaving the Python interpreter, this is helpful. The module object is the return value. Reloading modules in Python2.xreload(module)For above 2. x and <=Python3.3import imp imp.reload(module)
1 min read
Python | Opaque Pointers in C extension modules
Let's discuss an extension module that needs to handle a pointer to a C data structure, without exposing any internal details of the structure to Python. Opaque data structures can be easily handled by wrapping them inside capsule objects as shown in the code snippet below. Code #1 : typedef struct Point { double x, y; } Point; extern double distan
2 min read
10 Interesting modules in Python to play with
Python is a high level, interpreted and general-purpose dynamic programming language that focuses on code readability. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management. It is one of the world’s most popular, in-demand programming languages. This is for many reasons: It’s ea
7 min read
Create and Import modules in Python
In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and packages. A package is a collection of modules in dir
3 min read
How to Dynamically Load Modules or Classes in Python
Python provides a feature to create and store classes and methods and store them for further use. The file containing these sets of methods and classes is called a module. A module can have other modules inside it. Note: For more information, refer to Python Modules Example: A simple example of importing a module is shown below in which, there are
3 min read
Basics Of Python Modules
A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard library modules. Creating our module We will be crea
3 min read
Modules available for Serialization and Deserialization in Python
Python provides three different modules which allow us to serialize and deserialize objects : Marshal ModulePickle ModuleJSON Module 1. Marshal Module: It is the oldest module among these three. It is mainly used to read and write the compiled byte code of Python modules. Even we can use marshal to serialize Python objects, but it is not recommende
3 min read
Best Python Modules for Automation
Automation is an addition of technology that performs tasks with reduced human assistance to processes that facilitate feedback loops between operations and development teams so that iterative updates can be deployed faster to applications in production. There are different types of automation library in Python: GUI AutomationFull Stack AutomationT
3 min read
Import Modules From Another Folder in Python
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two ways: Using sys.pathUsing PythonPath. Create a mod
2 min read
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using the pip installer, We can also create our own pytho
4 min read
Practice Tags :