Modules and Packages in Python

Search for a command to run...

No comments yet. Be the first to comment.
Python 101 series will teach you all basics you need to get into Python world. Python series have all basic covered so you can at least do a google search.
Just to refresh our memory, so far, we have seen all basics of python including variables, data types, functions, conditional statements and loops. You can check out them all on my Python 101 series page. In every software development, we have to do ...
Unlock the Power of Python Comprehensions to Enhance Your Code's Clarity and Performance

Hello, friends! Today, I want to share why you should avoid sending mere "Hi!" or "Hey!" messages when trying to communicate with your colleagues or even friends. Having worked for three years, one immediate lesson I learned is that nobody has time t...

Python. You might be thinking I know what a list is, I have used it numerous times. But there is a difference between using a list and using a list efficiently. So, I will show you what I have learned as a Software Engineer. We can create a list basi...

As developers, we face many cases when we import data from one source and want to upload this data into another source. This was the same situation I was facing. I exported data from Notion and wanted to import it into the MongoDB Atlas. To insert da...

We have explored how to get current date, month, year and day of the week in the previous blog. In this blog post we will explore more of a datetime and its corresponding methods with some tutorial. Get the Current Week Number using Python There are ...

In real-life development, we do not write every single line of code. If there are any resources or libraries available we generally use those as long as the source is trustable. It is just like the old saying, "If something is available use it, don't need to reinvent the wheel."
So, in this tutorial, I will show you
How to install any third-party package
How to use any package (inbuilt and/or third-party)
Once we understand the basic concepts of packages in python. We will work on a small project in which you will take input from a user and save it in different file formats.
Before we understand what is a package, we need to understand what are modules in python. Because all packages are a combination of different modules. So, what is a module?
In real-life development, code files can be of hundreds or even thousands of lines. To manage and handle all code in one file is very difficult and not at all the suggested way to write the code. That is why we divide our code into different parts. These separate parts are called modules.
Now, after creating all the modules, we zip up all the related modules and get one package. So, the package is nothing but a combination of the related module(s). Most of the time, when the codebase became too large we create a package. But, you may see most of the time, people treat packages and modules the same way in python.
To install any third-party packages we need pip. Pip is a package manager for Python. If you are coming from a JavaScript background you might know about npm or yarn. So, before we install any packages in python we need to confirm if pip is installed on our system or not. Generally, the python installer installs pip alongside python. To verify pip installation, you can use the following command in your terminal
pip --version
If this command throws an error or won't show any pip version you can re-install pip using the following command.
python -m pip install --upgrade pip
After the installation process finishes check the pip version again. If you are still facing an issue mention issue in the comment section. I will try my best to help.
Syntax to install the package using pip:
pip install package_name
This is just a basic syntax there are plenty of operations that we can do with pip. Once we have verified pip installation, we need the name of the package. As we become more experienced developers, we tend to know which package to use and if we do not know we can always ask on online forums such as StackOverflow.
In this blog, I will show you the package that will help us to save data in excel (Xls) format. I did a quick google search and found we can write data into excel using two packages
xlsxwriter
pandas
There can be any other packages but I came across these two quite often. We can just write into excel format using xlsxwriter. To read excel files we need another package named xlrd. To install xlsxwriter use the following command.
pip install xlsxwriter
Now, if we do not face any errors while installing the package, let's verify the installation of the package. To verify you can open a terminal on your system and follow the steps shown in the image:

If xlsxwriter is not installed properly on your system, you will see something like this:

This is about third-party packages. But, how do we use inbuilt modules and packages, you ask? We can use inbuilt modules/packages the same way we use third-party packages. One difference is we do not have to install any modules/packages provided by python.
In python, we have csv and json named inbuilt packages. To use these packages we need to import them the same way we imported the xlsxwriter package.

Sometimes we need to upload our codebase to specific cloud services, and it is possible that we won't be able to install the module the same way we installed it on our local system. Amazon lambda is an example of such a cloud service. In this kind of scenario, we need to zip the module together with our code file. So, to install the module locally at the specific location we can use the following command:
pip install package_name -t PATH_TO_ROOT_FOLDER
Let's assume we want to install xlsxwriter in the current directory. To install/download xlsxwriter in the current directory we can use the following command in our terminal.
pip install xlsxwriter -t .
Here, full stop (.) represents the current directory. If you want, you can provide the full path to the current or specific directory as well. Once the package is downloaded we will see the following files in the current directory.

Finally! We are at the end of this section 😁.
In this blog, we understood how to install and use packages. But, there is more to package concepts in python. You can create and publish your own package. We will see this in some other blog.
I know, it is a lot to take in at a time. But, you don't need to remember everything that I have mentioned here. I just showed you so that you can recall what is possible and what is not. There are some other methods as well that I have not mentioned here.
That was it. Thank you for reading.
Let me know if you need any help or want to discuss something. Reach out to me on Twitter or LinkedIn. Make sure to share any thoughts, questions, or concerns. I would love to see them.
Till the next time 👋