
Python Package and PIP¶
- Managing external libraries in Python is made simple using PIP — Python’s default package manager.
What is PIP?¶
PIP stands for "Pip Installs Packages". It is the official package manager for Python, allowing you to:
Install packages (libraries/modules) from the Python Package Index (PyPI).
Manage dependencies required for your Python projects.
Uninstall or upgrade packages when needed.
What is a Python Package?¶
A Python package is a directory that contains multiple modules and includes an optional
__init__.py
file. In most cases:A module is a single
.py
file (Python script).A package is a collection of modules bundled for reuse.
These packages can be installed via PIP and imported into your project to extend its capabilities.
Checking if PIP is Installed¶
- To verify whether PIP is already installed on your system:
pip --version
- If you see a version number, it means PIP is installed.
In [ ]:
pip --version
pip 24.1.2 from /usr/local/lib/python3.11/dist-packages/pip (python 3.11)
Installing PIP (If Not Already Installed)¶
- If running
pip --version
throws an error, PIP might not be installed. To install it:
- Visit the official PIP installation page: https://pypi.org/project/pip/
- Alternatively, if using Python from python.org, PIP comes bundled with Python 3.4 and above.
Installing a Python Package using PIP¶
- To install a package, use the
pip install <package-name>
command.
pip install textblob
Using an Installed Package in Python¶
- Once a package is installed using PIP, it can be imported and used in your project to add powerful features.
Example: Sentiment Analysis with textblob
¶
In [5]:
# Make sure you've installed TextBlob using:
# pip install textblob
# Also, download necessary corpora (only once) using:
# python -m textblob.download_corpora
from textblob import TextBlob
# Sample text from a tutorial feedback
feedback = "I absolutely love the machine learning tutorials on Intensity Coding!"
# Create a TextBlob object
blob = TextBlob(feedback)
# Analyze the sentiment of the feedback
print("Polarity:", blob.sentiment.polarity) # Ranges from -1 (negative) to 1 (positive)
print("Subjectivity:", blob.sentiment.subjectivity) # Ranges from 0 (objective) to 1 (subjective)
# Output:
# Polarity: 0.625
# Subjectivity: 0.6
Polarity: 0.625 Subjectivity: 0.6
Searching for Packages on PyPI¶
- You can explore thousands of available packages on: https://pypi.org
Uninstalling a Package¶
- If you no longer need a package, use the uninstall command:
In [6]:
pip uninstall textbloby
Found existing installation: textblob 0.19.0 Uninstalling textblob-0.19.0: Would remove: /usr/local/lib/python3.11/dist-packages/textblob-0.19.0.dist-info/* /usr/local/lib/python3.11/dist-packages/textblob/* Proceed (Y/n)? y Successfully uninstalled textblob-0.19.0
- Press
y
to confirm and complete the uninstallation.
Listing All Installed Packages¶
- To see all installed Python packages in your environment:
pip list
Output¶
Package Version
------------------------------------- -------------------
absl-py 1.4.0
accelerate 1.8.1
aiofiles 24.1.0
aiohappyeyeballs 2.6.1
aiohttp 3.11.15
.............................................
.............................................
Upgrading an Existing Package¶
- To upgrade a package to the latest version:
pip install --upgrade pandas
- You can also upgrade pip itself:
pip install --upgrade pip