Python Packages and PIP
Python Packages and PIP
Packages and
PIP
DCSN03C – Computer Programming 2
Package
What is Python Package?
A Python package is a way to organize
related modules into a single unit. Think
of it like a folder or directory on your
computer, but instead of holding files, it
holds Python modules (which
themselves contain functions, classes,
and other code). Packages help you
structure your code and prevent naming
collisions when you have many modules.
How to create a package?
A Python package is simply a collection of modules (Python files) organized in a directory
with a special __init__.py file.
result = mathoperations.sum(5,3)
print(result)
PIP
Python packaging ecosystem
Python's power and versatility have made it
a popular tool across many disciplines, from
AI research to data mining and beyond. To
avoid redundant work, the Python
community emphasizes code sharing and
reuse. This is facilitated by a centralized
repository, PyPI (Python Package Index),
and tools to access it.
Python packaging ecosystem
PyPI, maintained by the Python Software Foundation,
is the primary hub for Python packages. It hosts a vast
number of projects and files, demonstrating the size
and activity of the Python community. While other
repositories exist, PyPI is the most significant, serving
as the central resource for Python packages. This
system allows developers to build upon existing work,
modify it as needed, and contribute their own code,
creating a continuous cycle of improvement and
innovation.
The PyPI repo: the Cheese Shop
PyPI, also known humorously as the "Cheese Shop" (a
reference to a Monty Python sketch), is a free and vast
repository of Python packages. Unlike a real shop, it
requires no payment. However, users are expected to
adhere to licensing terms. To access and utilize these
packages, a special tool called pip is used. pip is a
recursive acronym, standing for "pip installs packages"
(and the pip within that stands for "pip installs
packages," and so on). Essentially, pip is the tool that
allows users to install and manage packages from PyPI.
How to install PIP?
1. Check if PIP is Already Installed
Run the script:Open Command Prompt, navigate to the folder where you saved
the file, and run
get-pip.py
Dependencies
When using Python packages, it's common for them to rely on
other packages, creating dependencies. For example, your
redsuspenders app might depend on nyse, which in turn
depends on wallstreet, and so on. This chain of dependencies
means that anyone using your app also needs to install all the
dependent packages. Manually tracking and installing all
these dependencies is a tedious process known as
"dependency hell.“
Fortunately not – pip can do all of this for you. It can discover, identify, and resolve all
dependencies. Moreover, it can do it in the cleverest way, avoiding any unnecessary
downloads and reinstalls.
How to use pip
Now we’re ready to ask pip what it can do for us. Let's do it – issue the following command:
The list produced by pip summarizes all the available operations, and the last of them is help, which we've just used already.
How to use pip
If you’re a system administrator, you can install pygame using the following command: