{"id":72329,"date":"2023-07-18T06:13:52","date_gmt":"2023-07-18T06:13:52","guid":{"rendered":"https:\/\/itsourcecode.com\/?p=72329"},"modified":"2023-10-26T02:19:12","modified_gmt":"2023-10-26T02:19:12","slug":"python-module-vs-package-vs-library","status":"publish","type":"post","link":"https:\/\/itsourcecode.com\/python-tutorial\/python-module-vs-package-vs-library\/","title":{"rendered":"Library vs Module vs Package in Python: Differences and Examples"},"content":{"rendered":"\n
Are you curious what’s the difference between library vs. module vs. package in Python?<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n So, let’s dive in and demystify the difference between Python modules, packages, and libraries<\/strong> along with examples.<\/p>\n\n\n\n A module <\/strong>is like a container that holds different useful things such as:<\/p>\n\n\n\n <\/p>\n\n\n\n It’s a way to package these elements together so they can be easily used by many different programs.<\/p>\n\n\n\n <\/p>\n\n\n\n Rather than creating a separate program just to store these elements, a module can be included within another program.<\/p>\n\n\n\n This makes it convenient to reuse code across different projects.<\/p>\n\n\n\n <\/p>\n\n\n\n Basically, a module in Python is just a file with a .py extension that contains Python code.<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n For example, let\u2019s define a Python executable function to greet our visitors to a certain event:<\/p>\n\n\n\n <\/p>\n\n\n\n This code is saved in a file called \u201cwelcome.py\u201d because we want this function to be part of the \u201cwelcome\u201d module.<\/p>\n\n\n\n <\/p>\n\n\n\n We need to import the module using the import statement before we can use the code in our application. <\/p>\n\n\n\n <\/p>\n\n\n\n We can call the function defined in the module by using the\u00a0module.function()<\/em>\u00a0syntax.<\/p>\n\n\n\n <\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n It\u2019s possible to import only a specific function from a module, rather than the entire module.<\/p>\n\n\n\n To do this, you can use the following syntax:<\/p>\n\n\n\n <\/p>\n\n\n\n If you\u2019ve used Python before, it\u2019s likely that you\u2019ve used modules.<\/p>\n\n\n\n For instance, you might have used:<\/p>\n\n\n\n <\/p>\n\n\n\n In order for you to test your\u00a0Python code<\/strong>\u00a0provided in this lesson, you must test the code on your code editor like\u00a0PyCharm<\/strong>.<\/p>\n\n\n\n <\/p>\n\n\n\n But if you wish to run this code online, we also have an\u00a0Online Compiler in Python<\/strong><\/a>\u00a0or interpreter for you to test your\u00a0Python code<\/strong>\u00a0for free.<\/p>\n\n\n\n The package <\/strong>is a directory containing collections of modules, including an\u00a0__init__.py<\/strong>\u00a0file that tells the interpreter to treat it as a package.<\/p>\n\n\n\n <\/p>\n\n\n\n Additionally, a package is simply a namespace that can also contain sub-packages\u00a0and modules.<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n For example, we can put modules related to our data science project in the following package:<\/p>\n\n\n\n <\/p>\n\n\n\n The dot notation lets us import only certain modules from this package. <\/p>\n\n\n\n <\/p>\n\n\n\n For example, we can use one of the following code snippets to import the\u00a0download\u00a0module from the\u00a0Excel<\/em>\u00a0sub-package above:<\/p>\n\n\n\n <\/p>\n\n\n\n Next, we could choose to import only the file_download()<\/em> function from our download.py<\/em> module. Either of these options will work:<\/p>\n\n\n\n <\/p>\n\n\n\n You probably already use a lot of the built-in and open-source Python packages.<\/p>\n\n\n\n For instance:<\/p>\n\n\n\n As your application gets bigger and uses more modules, Python packages become an important part of optimizing the structure of your code.<\/p>\n\n\n\n <\/p>\n\n\n\n We have included an\u00a0__init__.py<\/strong>\u00a0file inside a directory to tell Python that this directory is a package.<\/p>\n\n\n\n <\/p>\n\n\n\n You need to include this file whenever you want to create a package. Depending on your preferences, you can either write code inside of it or leave it blank.<\/p>\n\n\n\n <\/p>\n\n\n\n To make a package in Python, follow the steps below.<\/strong><\/p>\n\n\n\n A\u00a0library\u00a0<\/strong>is a collection of code, usually related modules and packages. This term is often used conversely with the \u201cPython package\u201d.<\/p>\n\n\n\n <\/p>\n\n\n\n However many people think that a package is a collection of modules and that a library is a collection of packages.<\/p>\n\n\n\n <\/p>\n\n\n\n Developers create Python libraries to share code with others. This way, they don’t have to write code from scratch every time they need a specific function.<\/p>\n\n\n\n <\/p>\n\n\n\n Today, there are a lot of useful libraries to choose from. I’ll give just a few examples:<\/p>\n\n\n\n <\/p>\n\n\n\n The\u00a0Python Standard Library\u00a0<\/strong>is a collection of Python\u2019s core syntax, tokens, and semantics. It comes bundled with the Python distribution.<\/p>\n\n\n\n <\/p>\n\n\n\n Python is a language that is written in C and handles core functionality like I\/O and other modules.<\/p>\n\n\n\n <\/p>\n\n\n\n The core of the standard library is made up of more than\u00a0200 core modules<\/strong>. This library is already part of Python.<\/p>\n\n\n\n <\/p>\n\n\n\n A library in Python<\/strong> is a collection of modules or packages that provide specific functionality for various tasks.<\/p>\n\n\n\n <\/p>\n\n\n\n Modules<\/strong> are single files containing Python code that define functions, classes, and variables, which can be imported and used in other programs.<\/p>\n\n\n\n <\/p>\n\n\n\n Packages<\/strong> are a way of organizing related modules into a directory hierarchy, allowing for better organization and avoiding naming conflicts. <\/p>\n\n\n\n <\/p>\n\n\n\n Packages <\/strong>typically consist of multiple modules and sub-packages and are represented by directories with an init<\/strong>.py file.<\/p>\n\n\n\n <\/p>\n\n\n\n Overall, libraries <\/strong>encompass multiple modules <\/strong>or packages<\/strong>, modules are individual code files<\/em>, and packages are a way to organize and structure related modules in a directory hierarchy<\/em>.<\/p>\n\n\n\n <\/p>\n\n\n\n This tutorial has discussed library vs module vs package Python differences<\/strong>. I have high hopes that you will acquire new knowledge from reading this article.<\/p>\n\n\n\n <\/p>\n\n\n\nWhat is a Python Module?<\/strong><\/h2>\n\n\n\n
\n
\n
\n
\n
def greeting_message(event_name):\n print(\"Welcome guest to our \" + event_name+ \". Before proceeding, let's take a pause and watch the beginning of the show.\")<\/strong><\/code><\/pre>\n\n\n\nimport welcome\n\nwelcome.greeting_message (\u201cPython Module Tutorial\u201d)<\/strong><\/code><\/pre>\n\n\n\nWelcome guest to our Python Module Tutorial.<\/strong><\/code><\/pre>\n\n\n\nfrom welcome import greeting_message<\/strong><\/code><\/pre>\n\n\n\n\n
\n
\n
\n
What is a Python Package?<\/strong><\/h2>\n\n\n\n
<\/a>import export.excel.download\nor\nfrom export.excel import download<\/strong><\/code><\/pre>\n\n\n\nimport export.excel.download.load_dataset()\nor\nfrom export.excel.download import file_download()<\/strong><\/code><\/pre>\n\n\n\n\n
\n
\n
Creating a Package<\/strong><\/h3>\n\n\n\n
\n
\n
\n
What is a Python<\/strong> Library?<\/strong><\/h2>\n\n\n\n
\n
Python Standard Library<\/strong><\/h3>\n\n\n\n
Difference between Python Modules, Packages and Libraries<\/strong><\/h2>\n\n\n\n
Summary<\/strong><\/h2>\n\n\n\n