Modular Modules and Packages in Python
Tags: beginners, programming, tutorial, python
Published: 2022-09-22 09:02:41

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

1. How to install any third-party package
2. 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 `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

```bash
pip --version
```

If this command throws an error or won't show any pip version you can re-install pip using the following command.

```bash
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:

```bash
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

1. xlsxwriter
2. 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.

```bash
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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ibprz1ryt8r5d6gs6bj.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rn43dky15wqtldhkq4v3.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65kunzk3cxe7r1zhzmaq.png)
# 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:

```bash
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.

```python
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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7dg84t5vvcf0jrystw3.png)

# Conclusion

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](https://twitter.com/sahil_fruitwala) or [LinkedIn](https://www.linkedin.com/in/sahilfruitwala). Make sure to share any thoughts, questions, or concerns. I would love to see them.

Till the next time 👋