Python Package Managers - Installing Packages for New Functions
Python’s ecosystem is largely powered by third-party packages that extend the language beyond its built-in features. These packages are distributed through package managers such as PyPI (Python Package Index), where developers can discover, install, and manage libraries created by the global Python community. In this post, I’ll cover the fundamentals of Python packages and share what I’ve learned about installing and using them in practice.
In an earlier post, I wrote about using imported modules that bring new functions into our code. To further enhance the potential, you can also download packages that contain additional modules using Package Managers. Each package you download will serve its own unique purpose and house several different functions that are all related, as with normal import modules.
The only real difference here is that you download and install these specific modules to achieve certain goals. You won’t always use the same downloaded packages in two different projects.
With all that said, I’ll now begin to relearn all about Python packages and how to use them.
Getting Started
Before we can begin, we need to make sure that our local machines, laptops/computers, have pip installed. Pip is an installer program, which is an abbreviation of Pip Installs Python or Preferred Installer Program.
Note: ChatGPT mentioned that we should refrain from using the following code, as Python comes pre-installed or bundled with this already; however, it’s included in the course and I want to include as much as I have learnt from there.
1
pip install pip
We wait for this install to go through and then run a version check to see if we have the most up-to-date version installed.
1
python -m pip --version
The output should look something like:
pip 25.3 from C:\Python314\Lib\site-packages\pip (python 3.14)
This tells me I do have pip installed, and I can start downloading and installing packages.
Seeking Packages to Install
When I first learnt about installing packages, it was a topic introduced without an introduction, which made it feel like the norm. I was glad to go over courses like the 30 Days of Python by Asabeneh, as it gave me the proper insights I needed to understand what Python Packages were.
I must also mention that all the packages you’ll install can be created entirely by yourself instead. We only really install packages to make coding easier.
To begin, we would visit:
Python Package Index / pypi.org
As this is the official Python Package Index library, we search for the package we require.
For this post, we’ll go through installing a package that helps enhance mathematical operations in Python, Numpy.
We can skip searching through PyPi.org as we already know the package we want to install, and just go on to our terminal and insert the following:
1
pip install numpy
Once the installation is complete, we can now load our code editor, VS Code for me, and begin to import this module in to our Python file for use like usual.
1
import numpy
💡 Tip: Once a package is installed, VS Code provides IntelliSense that allows you to explore all available functions and attributes by typing the module name followed by a period (for example, numpy.). The official Python extension powers this and is a cleaner alternative to using print(dir(numpy)). 👉 Alternate Methods
Using Installed Packages
Now, all the numpy functions have been made available to us.
To portray a contrast between using numpy and not, I’ll include two examples of the same functions being performed. One with numpy and the other without.
1
2
3
nums = [1,2,3,4,5]
doubled = [x * 2 for x in nums]
print(doubled)
Output:
[2, 4, 6, 8, 10]
1
2
3
import numpy as np
arr = np.array([1,2,3,4,5])
print(arr * 2)
Output:
[2, 4, 6, 8, 10]
Instead of having to use list comprehension, we could use numpy’s array function to perform this task for us, and it took fewer lines of code to do. Once we begin to code, even as a beginner, we want to try and get used to writing efficient code. This is to ensure we minimise the amount of debugging that could be required later on and refactoring.
Other PIP Commands
That pretty much sums it all up for us. Sounds daunting at first, but really, it’s like downloading anything from the web and using it.
I’ll use this opportunity to go over some other console or terminal commands for PIP which are important and will be used when it comes to creating virtual environments for my Python projects.
Note: Virtual Environments or VENV made for Python projects is like creating a snapshot of all the imported and downloaded packages with their versions and having them installed on a virtual machine inside your laptop, and keeping them on those versions. This way, if there are any updates released and installed on your device, it won’t transfer over to your virtual environment unless you update them manually in there - I will write about venv in a later post.
Uninstall a Package
If we ever want to uninstall a package, we would perform the following code on our terminal:
1
pip uninstall packagename
For example:
1
pip uninstall numpy
List All Packages
If you’d like to see a list of all packages on your virtual environment or device:
1
pip list
Package Information
If you’d like to see more details about a certain package:
1
pip show packagename
For example:
1
pip show numpy
Output:
1
2
3
4
5
6
7
8
9
10
Name: numpy
Version: 2.4.0
Summary: Fundamental package for array computing in Python
Home-page: https://numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License-Expression: BSD-3-Clause AND 0BSD AND MIT AND Zlib AND CC0-1.0
Location: C:\Python314\Lib\site-packages
Requires:
Required-by: contourpy, matplotlib, pandas, scikit-learn, scipy
If you’d like to see more detail:
1
pip show numpy --verbose
Freezing a List of Packages
This is important to know when it comes to creating a virtual environment for Python projects, as this is what you’ll use to freeze a list of required packages and their versions and then have those exact ones installed on the virtual environment.
As promised, this will be covered in a later post; for now, I will just introduce the basic command:
1
2
pip freeze > filename.txt
# Freeze all Python packages and their versions into filename.txt
Output:
1
2
3
4
5
numpy==2.4.0
Jinja2==2.7.2
MarkupSafe==0.19
Pygments==1.6
Sphinx==1.2.2
You’ll then install on your virtual environment using the .txt file it produces.
Creating Our Own Package
Like with creating modules, you can create your own package. That’s what PyPi.org is, a collection of different developers’ work.
The difference between a package and a module is that you can have many functions/attributes within a module, but in a package, you can also have many modules with many attributes/functions.
To do this, you would create a directory/folder with all your modules inside:
IMPORTANT: Please ensure that you have created an empty init.py and placed this inside the directory too.
This is how your package folder should look:
1
2
3
4
─ mypackage/
├── __init__.py
├── module1.py
└── module2.py
Now create your modules with all the functions inside:
1
2
3
4
5
6
7
#module1.py and module2.py (you can name the file anything)
def function1():
code
def function2():
code
def function3():
code
Finally, import and use them like any other module or package:
1
2
3
4
5
6
7
from mypackage import *
from mypackage import module1
import mypackage
import mypackage as mp
import mypackage.module1
# All permutations of importing
Summary
Well, that does it. That’s all we really need to know when it comes to using packages in Python and what we can do with them. As I progress through the rest of the course, I will be using more packages and get to grips with using them. The key takeaways for me were:
- We can install all sorts of packages that have their own unique set of functions
- Learning how to use pip to install packages
- How to get a list of all installed packages and their versions
- How to create my own package
I hope that this post has been as insightful for you as it has been for me. I’ll be sure to update the posts with the relevant links included when I have the updates made.