Modules and Packages in Python
- Published on •
- Reading Time
- ⌛ 6 Min Read
- Sahil Fruitwala
Table of Contents
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.
What is a Module in Python?
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.
What is a Module in Python?
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.
How to install a Package / Module in Python?
To install any third-party packages we need
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
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
How to use Package / Module in Python?
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
json named inbuilt packages. To use these packages we need to import them the same way we imported the xlsxwriter package.
Save all files of package locally
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 👋