0% found this document useful (0 votes)
21 views18 pages

Python Packages and PIP

The document explains Python packages, which are collections of related modules organized in a directory, and details the steps to create a package. It also covers the Python packaging ecosystem, emphasizing the role of PyPI (Python Package Index) as a central repository for Python packages and the use of pip for package management. Additionally, it discusses the concept of dependencies in Python packages and how pip can manage them efficiently.

Uploaded by

Lance CPM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

Python Packages and PIP

The document explains Python packages, which are collections of related modules organized in a directory, and details the steps to create a package. It also covers the Python packaging ecosystem, emphasizing the role of PyPI (Python Package Index) as a central repository for Python packages and the use of pip for package management. Additionally, it discusses the concept of dependencies in Python packages and how pip can manage them efficiently.

Uploaded by

Lance CPM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Python

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.

Step 1: Create a folder for your main program file


Make a new folder for your program, example: MyProgram
How to create a package?
Step 2: Create the Package Directory
Make a new folder for your package, ex: mypackage.
How to create a package?
Step 3: Add an __init__.py File
Inside mypackage/, create an empty __init__.py file. This tells
Python that the folder is a package.
How to create a package?
Step 4: Add Your ModulesCreate Python files (modules) inside
mypackage/. Example:
How to create a package?
Step 5: Import and Use the Package
Now, you can import your package in another script

from mypackage import mathoperations, greet

Name = input("Enter your name: ")


greet.displayMsg(Name)

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

If PIP is installed, you’ll see output like:


How to install PIP?
If PIP is not installed, follow the steps

Method 1: Using ensurepip


If you already have Python installed, open the command prompt and run:

python -m ensurepip --default-pip


How to install PIP?
Method 2: Using get-pip.py
Download the get-pip.py script:
Open this link: https://bootstrap.pypa.io/get-pip.py
Save the file to your computer.

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:

pip install pygame

You might also like