Back to articles list July 15, 2021 - 8 minutes read Difference Between Python Modules, Packages, Libraries, and Frameworks Kateryna Koidan Kateryna is a data science writer from Kyiv, Ukraine. She worked for BNP Paribas, the leading European banking group, as an internal auditor for more than 6 years. More recently, she decided to pursue only the favorite part of her job—data analysis. Now she is continuing her self-education with deep-learning courses, enjoys coding for data analysis and visualization projects, and writes on the topics of data science and artificial intelligence. Kateryna is also a proud mother of two lovely toddlers, who make her life full of fun. Tags: python python basics If you are new to Python, you might be confused about all its libraries, packages, modules, and frameworks. From the context, you can usually understand that these are some pieces of code. But what’s the difference between them? In this article, I’ll explain the difference between Python modules, packages, libraries, and frameworks in simple terms. Real-world programs are complex. Even a simple game like a dice roll simulator would require lots of code if you programmed everything from scratch. To simplify the process and make it more effective, developers leverage modular programming – a method of breaking large coding tasks into smaller and more manageable subtasks. This is why Python has so many modules, packages, libraries, and frameworks. Now, let’s dive deeper into each of these concepts so we can understand the differences. Python Modules If you want your code to be well organized, it’s a good idea to start by grouping related code. A module is basically a bunch of related code saved in a file with the extension .py. You may choose to define functions, classes, or variables in a module. It’s also fine to include runnable code in modules. For example, let’s define a function to welcome new students to a particular course: def welcome_message(course): print("Thank you for subscribing to our " + course + " course. You will get all the details in an email shortly.") To have this function stored in the module welcome, we save this code in a file named welcome.py. If we want to use this code in our application, we first need to import the respective module using the import statement. Then, we’ll be ready to use a function defined in this module by calling that function with the module.function() syntax: import welcome welcome.welcome_message (“Python Basics Part 1”) Output Thank you for subscribing to our Python Basics Part 1 course. You will get all the details in the email shortly. It’s common to have many different items defined within the same module. So, you may want to import only one specific function rather than the entire module. For that, you can use the following syntax: from welcome import welcome_message If you have some experience with Python, you’ve likely used modules. For example, you may have used the: random module to generate pseudo-random number generators for various distributions. html module to parse HTML pages. datetime module to manipulate date and time data. re module to detect and parse regular expressions in Python. Modules introduce numerous benefits into your Python code: Improved development process. Python modules help you focus on one small portion of a task rather than an entire problem. This simplifies the development process and makes it less prone to errors. Furthermore, modules are usually written in a way that minimizes interdependency. Thus, it’s more viable for a team of several programmers to work on the same application. The functionality you define in one module can be used in different parts of an application, minimizing duplicate code. Separate namespaces. With Python modules, you can define separate namespaces to avoid collisions between identifiers in different parts of your application. Python Packages When developing a large application, you may end up with many different modules that are difficult to manage. In such a case, you’ll benefit from grouping and organizing your modules. That’s when packages come into play. Python packages are basically a directory of a collection of modules. Packages allow the hierarchical structure of the module namespace. Just like we organize our files on a hard drive into folders and sub-folders, we can organize our modules into packages and subpackages. To be considered a package (or subpackage), a directory must contain a file named __init__.py. This file usually includes the initialization code for the corresponding package. For example, we can have the following package my_model with modules related to our data science project: We can import specific modules from this package using the dot notation. For example, to import the dataset module from the above package, we can use one of the following code snippets: import my_model.training.dataset OR from my_model.training import dataset Next, we may choose to import only the load_dataset() function from our dataset.py module. Either of the following options will do the job: import my_model.training.dataset.load_dataset() OR from my_model.training.dataset import load_dataset() There are a lot of built-in and open-source Python packages that you are probably already using. For example: NumPy is the fundamental Python package for scientific computing. pandas is a Python package for fast and efficient processing of tabular data, time series, matrix data, etc. pytest provides a variety of modules to test new code, including small unit tests or complex functional tests. As your application grows larger and uses many different modules, Python packages become a crucial component for optimizing your code structure. Python Libraries A library is an umbrella term referring to a reusable chunk of code. Usually, a Python library contains a collection of related modules and packages. Actually, this term is often used interchangeably with “Python package” because packages can also contain modules and other packages (subpackages). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages. Oftentimes, developers create Python libraries to share reusable code with the community. To eliminate the need for writing code from scratch, they create a set of useful functions related to the same area. There are thousands of useful libraries available today. I’ll give just a few examples: Matplotlib library is a standard library for generating data visualizations in Python. It supports building basic two-dimensional graphs as well as more complex animated and interactive visualizations. PyTorch is an open-source deep-learning library built by Facebook’s AI Research lab to implement advanced neural networks and cutting-edge research ideas in industry and academia. pygame provides developers with tons of convenient features and tools to make game development a more intuitive task. Beautiful Soup is a very popular Python library for getting data from the web. The modules and packages inside this library help extract useful information from HTML and XML files. Requests is a part of a large collection of libraries designed to make Python HTTP requests simpler. The library offers an intuitive JSON method that helps you avoid manually adding query strings to your URLs. missingno is very handy for handling missing data points. It provides informative visualizations about the missing values in a dataframe, helping data scientists to spot areas with missing data. It is just one of the many great Python libraries for data cleaning. By the way, the NumPy and pandas packages that were mentioned before are also often referred to as libraries. That is because these are complex packages that have wide applications (i.e. scientific computing and data manipulation, respectively). They also include multiple subpackages and so basically satisfy the definition of a Python library. Learn about other important libraries for data science in this article. Python Frameworks Similar to libraries, Python frameworks are a collection of modules and packages that help programmers to fast track the development process. However, frameworks are usually more complex than libraries. Also, while libraries contain packages that perform specific operations, frameworks contain the basic flow and architecture of the application. If you compare application development to house construction, Python frameworks provide all the essential building blocks like the foundation, walls, windows, and roof. Then, the developers build their application around this foundation by adding functionalities comparable to a house’s alarm system, furniture, appliances, etc. For a better understanding, let’s review several popular frameworks: Django is a Python framework for building web applications with less coding. With all the necessary features included by default, developers can focus on their applications rather than dealing with routine processes. Flask is a web development framework that is known for its lightweight and modular design. It has many out-of-the-box features and is easily adaptable to specific requirements. Bottle is another lightweight framework for web development that was originally meant for building APIs. Its unique features are that it has no dependencies other than the Python Standard Library and it implements everything in a single source file. Python frameworks allow programmers to streamline the web development process by providing a necessary foundation while still being flexible. No wonder that top applications – including Netflix, Airbnb, Reddit, and Udemy – leverage the benefits of Python frameworks. Time to Practice Your Python Skills! There are many valid reasons to learn Python: it’s beginner-friendly, its syntax is easy to learn, and a fantastic community is there to help you when you are stuck. And if that was not enough, consider the richness of Python’s ecosystem: think of any random task, and there’s a good chance someone has already built an open-sourced module or package to solve this problem. Are you ready to join this friendly community? Then I recommend starting with our basic track, Learn Programming with Python. It includes 5 interactive courses with 414 coding challenges that cover Python basics, including: Functions, loops, and conditional statements. Python lists, tuples, sets, and dictionaries. Python’s built-in algorithms. Handling errors, etc. If you are considering a data science career and want to learn Python in that context, the Python for Data Science track is what you need. This track also has 5 interactive courses and hundreds of coding challenges so you can master the tools you’ll need to work efficiently with data in Python. At LearnPython.com, our interactive courses mean you’ll gain knowledge and immediately put it into practice. You’ll write real code and check it in our real-time platform. You don't need to install or set anything up. It couldn't be easier to learn to write Python code that you can later apply to your own projects. So are you ready to learn Python? Thanks for reading, and happy learning! Tags: python python basics You may also like The Most Helpful Python Data Cleaning Modules Data cleaning is a highly critical task in data science. In this article, we review the most common data cleaning libraries for Python. Read more Visualizing Time Series Data with the Python Pandas Library Find out how to analyze stock prices for previous years and see how to perform time resampling, and time shifting with Python pandas. Read more Python Coding Best Practices and Style Guidelines You've spent hours studying Python, and you may even have several successful projects in your portfolio. But do you write your Python code like a pro? Let's review some important guidelines to help you clean up your code. Read more 5 Best Python IDEs for Data Science Installing Python isn't enough—you also need a good interactive development environment (IDE) to program in. So what are the best Python IDEs for data science? Let's find out! Read more Subscribe to our newsletter Join our weekly newsletter to be notified about the latest posts.