Modules and Packages in Python

Learn basics of modules and packages and how to use them in pythonModules and Packages in PythonIn 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…


This content originally appeared on Level Up Coding - Medium and was authored by Sahil Fruitwala

Learn basics of modules and packages and how to use them in python

Learn basics of modules and package and how to use them in python
Modules and Packages in Python

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 Package 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

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

  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.

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 csv and 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 the Package locally in Python

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(s) 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 a 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 .
# There is a space between "-t" and full stop(.)

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.

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 or LinkedIn. Make sure to leave any thoughts, questions, or concerns in the comments below. I would love to see them.

Want to learn more?
Signup for my Newsletter and get the best articles into your inbox.

Till the next time 👋

Explore my other blogs from Python 101 series

Modules and Packages in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Sahil Fruitwala


Print Share Comment Cite Upload Translate Updates
APA

Sahil Fruitwala | Sciencx (2022-06-12T17:06:24+00:00) Modules and Packages in Python. Retrieved from https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/

MLA
" » Modules and Packages in Python." Sahil Fruitwala | Sciencx - Sunday June 12, 2022, https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/
HARVARD
Sahil Fruitwala | Sciencx Sunday June 12, 2022 » Modules and Packages in Python., viewed ,<https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/>
VANCOUVER
Sahil Fruitwala | Sciencx - » Modules and Packages in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/
CHICAGO
" » Modules and Packages in Python." Sahil Fruitwala | Sciencx - Accessed . https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/
IEEE
" » Modules and Packages in Python." Sahil Fruitwala | Sciencx [Online]. Available: https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/. [Accessed: ]
rf:citation
» Modules and Packages in Python | Sahil Fruitwala | Sciencx | https://www.scien.cx/2022/06/12/modules-and-packages-in-python-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.