Scripts, Modules, Packages, and Libraries
Python uses some terms that you may not be familiar with if you’re coming from a different language. Among these are scripts, modules, packages, and libraries.
A script is a Python file that’s intended to be run directly. When you run it, it should do something. This means that scripts will often contain code written outside the scope of any classes or functions.
A module is a Python file that’s intended to be imported into scripts or other modules. It often defines members like classes, functions, and variables intended to be used in other files that import it.
A package is a collection of related modules that work together to provide certain functionality. These modules are contained within a folder and can be imported just like any other modules. This folder will often contain a special
__init__ file that tells Python it’s a package, potentially containing more modules nested within subfolders
A library is an umbrella term that loosely means “a bundle of code.” These can have tens or even hundreds of individual modules that can provide a wide range of functionality. Matplotlib is a plotting library. The Python Standard Library contains hundreds of modules for performing common tasks, like sending emails or reading JSON data. What’s special about the Standard Library is that it comes bundled with your installation of Python, so you can use its modules without having to download them from anywhere.
These are not strict definitions. Many people feel these terms are somewhat open to interpretation. Script and module are terms that you may hear used interchangeably.
You can learn more about packages in Python Modules and Packages: An Introduction.
00:00 Python uses a lot of fancy terminology that you may not be familiar with if you’re new to programming or if you come from a different language. Before Python, my background was primarily in C# and Microsoft’s .NET framework.
00:30 A Python script is best defined as a Python file that contains commands in logical order. For this to be considered a script, the program should do something—really, anything—when it’s run through the Python interpreter. To better understand this, take a look at these two programs.
00:51 Both programs contain a function that will return the sum of two values. However, only the program on the right will actually call that function and print its result. The file on the right is intended to be run directly, so we can say that it’s a Python script. On the other hand, the program on the left doesn’t look like it’s actually meant to do anything.
01:16 Running that file will not produce any sort of output, since the function is never being called. It’s not intended to be run directly. The file on the left is better classified as a module. That’s because we can import it into other files to utilize its classes, functions, variables, and other members it’s defined.
I’m going to change the program on the right so that instead of defining the
add() function, we import it from the module on the left. This has the effect of making the addition function on the left accessible to the script on the right.
01:58 So, long story short: scripts are intended to be run directly, whereas modules are meant to be imported. Scripts often contain statements outside of the scope of any class or function, whereas modules define classes, functions, variables, and other members for use in scripts that import it.
Packages are a collection of related modules. A package can be an individual Python module, but they’re most often a collection of multiple related modules that come bundled together. This here is the
dateutil package, which allows for performing advanced date-related operations.
03:13 These are modules because they don’t do anything interesting on their own. When you import one into your own script, nothing really happens. You have to use something defined within the module, such as a class, function, or a variable.
03:42 We can’t talk about libraries without mentioning the Python standard library. The Python standard library is a large collection of modules and packages that come bundled with your installation of Python.
That means that you don’t have to download them from anywhere—you can just import them into your scripts and start using them right away.
dateutil is an example of a standard library package, but there are also others, like
04:16 The key takeaway here is that a lot of this is, to some extent, subjective. Scripts are runnable Python programs that do something when executed. Modules are Python files that are intended to be imported into scripts and other modules so that their defined members—like classes and functions—can be used.
04:41 Packages are a collection of related modules that aim to achieve a common goal. Finally, the Python standard library is a collection of packages and modules that can be used to access built-in functionality. In an ideal world, you’d import any necessary modules into your Python scripts without any issues.
05:04 Unfortunately, that’s often not the case. In the next video, I’ll show you what to do when Python can’t find the module you’ve just installed. Then, you’ll learn how to actually install them with Python’s package manager.
Become a Member to join the conversation.