Introduction to pip Functionality in Python 3 and Installation Guide

1. Pre-Learning Insights

As we delve deeper into learning Python, we notice a phenomenon: the Python language seems not as simple as it is said to be! Whether it is classes/objects or iterators/generators, it can be somewhat profound.

In fact, saying that Python is simple is relative to previous languages like C and Java, which have stricter definitions and uses for data types and variables. Python, on the other hand, can be used directly without explicit definitions. But why does it seem less simple as we progress in our Python studies?

This is because any programming language is aimed at solving problems. The demands of the real world are diverse, such as those requiring communication, databases, multithreading, web interfaces, etc. Any programming language must handle these different domains accordingly.

Here comes Python’s advantage! Due to its widespread application and support worldwide, a vast support network has developed third-party modules that can accomplish almost all the functionalities you need. In other words, if you need to connect to a database, there are modules like pymysql to support you; if you need multithreading, there is the threading module, and so on.

These modules come with a rich set of functions that you can call to assist you in developing the required functionalities.

This leads us to another question: how do we introduce third-party modules? This is what this article will discuss regarding the pip functionality. With this tool, we can easily obtain third-party modules.

2. Introduction to pip Functionality and Its Usage

2.1 What is pip

lpip = “Package Installer for Python”

lOfficial standard for searching, downloading, installing, upgrading, and uninstalling third-party libraries (from the PyPI repository)

lPython ≥3.4 comes with it by default, and the executable file is called pip3, but it usually also maps to pip

2.2 First, check if it is installed

# Type the following command in the cmd terminal:

python -m pip –version

# If you see something like pip 23.x from …, it indicates it is already installed

Introduction to pip Functionality in Python 3 and Installation Guide

If it prompts No module named pip, proceed to the next step to install.

2.3 If not installed, install it (one command for each system)

Operating System

Command

Windows

python -m ensurepip –upgrade

Debian/Ubuntu

sudo apt update && sudo apt install python3-pip

macOS (Homebrew)

brew install python # This will also install pip3

Universal Script (curl directly)

curl -sS https://bootstrap.pypa.io/get-pip.py | python3

2.4 Common pip commands examples

Scenario

Command Example

Install the latest version

<span>python -m pip install requests</span>

Specify version

<span>python -m pip install django==4.2</span>

Upgrade package

<span>python -m pip install -U numpy</span>

Uninstall package

<span>python -m pip uninstall pillow</span>

Batch install dependencies

<span>python -m pip install -r requirements.txt</span>

Export current environment

<span>python -m pip freeze > requirements.txt</span>

View installed packages

<span>python -m pip list</span>

Check for outdated packages

<span>python -m pip list --outdated</span>

2.5 Package dependencies for easy deployment

python -m pip freeze > requirements.txt

The above command can list all the dependency packages used in our project and save them into a text file. When we port the program, having this dependency file allows us to install it on other machines by executing the following command:

python -m pip install -r requirements.txt

3. Conclusion

pip is equivalent to Python’s App Store; memorize the template “python -m pip action package_name” for installing/upgrading/uninstalling/exporting; combined with the venv virtual environment, it keeps project dependencies clean and portable.

Let us maintain our enthusiasm for learning and practice more. See you in the next issue!

Introduction to pip Functionality in Python 3 and Installation Guide

#python

Leave a Comment