PIP is the standard package manager for Python. It is the most commonly used tool for installing and managing Python libraries and packages.

The most common way of using pip is via the command interface. For example to install a package called 'foobar' with pip, you would use the following syntax in your commandline/shell:  

pip install foobar

In some cases, however, you may need to run PIP within a program, rather than from the command line. This can be the case, for example, when you want to automate installation and package management tasks rather than doing them manually.

We will begin with the officially recommended approach.

Calling pip as a subprocess

The subprocess module in the standard library provides a way to create new processes, execute system commands, and generally interact with them. By utilizing subprocess, you can call pip as a subprocess and perform package management tasks programmatically from your Python script. To do this, the pip package should be installed and in system PATH

The module provides the  subprocess.call() function which is used to run a command in the system shell.

subprocess.call(['pip', [arg1, arg2, arg3, .......)

The commands are given as an iterable such as a list containing the arguments, flags and option, just like you would type on the command line.

Installing a package:

subprocess.call(['pip', 'install', 'package_name', .....])
import subprocess

subprocess.call(['pip', 'install', 'requests==2.26.0'])

Collecting requests==2.26.0
  Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB)
     ---------------------------------------- 62.3/62.3 kB 416.1 kB/s eta 0:00:00

..............
Installing collected packages: requests
  Attempting uninstall: requests
    Found existing installation: requests 2.31.0
    Uninstalling requests-2.31.0:
      Successfully uninstalled requests-2.31.0
Successfully installed requests-2.26.0 

Installing from a requirements.txt
subprocess.call(['pip', 'install', '-r', 'requirements.txt'])

Uninstalling a package 

subprocess.call(['pip', 'uninstall', 'package_name'], .....)
import subprocess

subprocess.call(['pip', 'uninstall', 'requests'])

Found existing installation: requests 2.26.0
Uninstalling requests-2.26.0:
  Would remove:
    c:\users\*\appdata\local\programs\python\python311\lib\site-packages\requests-2.26.0.dist-info\*
    c:\users\*\appdata\local\programs\python\python311\lib\site-packages\requests\*
Proceed (Y/n)? y
  Successfully uninstalled requests-2.26.0

Checking broken dependencies

import subprocess

print(subprocess.call(['pip', 'check']))

No broken requirements found. 

listing all the installed module

import subprocess

subprocess.call(['pip', 'freeze'])
​
import subprocess

subprocess.call(['pip', 'list'])

The pip module

Pip is common to most python developers as just a command line tool, however, there is the pip module in the standard library which provides the interface for using pip. The module defines the main() function which is actually called when you run pip from the command line. By invoking the main() function within a script, you can programmatically execute pip commands.  

pip.main(command_list)

The command_list argument is a list of the command as you would run them from the command line, for example to install a package you would use the following syntax.

pip.main(['install', 'package_name'])
import pip 

pip.main(['install', 'pandas'])

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Collecting pandas
  Downloading pandas-2.0.2-cp311-cp311-win_amd64.whl (10.6 MB)
     ---------------------------------------- 10.6/10.6 MB 289.7 kB/s eta 0:00:00
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\*\appdata\local\programs\python\python311\lib\site-packages (from pandas) (2.8.2)
Collecting pytz>=2020.1
  Downloading pytz-2023.3-py2.py3-none-any.whl (502 kB)
     ---------------------------------------- 502.3/502.3 kB 431.7 kB/s eta 0:00:00
Collecting tzdata>=2022.1
  Downloading tzdata-2023.3-py2.py3-none-any.whl (341 kB)
     ---------------------------------------- 341.8/341.8 kB 471.7 kB/s eta 0:00:00
Collecting numpy>=1.21.0
  Using cached numpy-1.24.3-cp311-cp311-win_amd64.whl (14.8 MB)
Requirement already satisfied: six>=1.5 in c:\users\*\appdata\local\programs\python\python311\lib\site-packages (from python-dateutil>=2.8.2->pandas) (1.14.0)
Installing collected packages: pytz, tzdata, numpy, pandas
Successfully installed numpy-1.24.3 pandas-2.0.2 pytz-2023.3 tzdata-2023.3

Similarly, to uninstall a package, you would need to run pip.main(['uninstall', 'package_name'])

import pip

pip.main(['uninstall', 'pandas'])

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Found existing installation: pandas 2.0.2
Uninstalling pandas-2.0.2:
  Would remove:
    c:\users\*\appdata\local\programs\python\python311\lib\site-packages\pandas-2.0.2.dist-info\*
    c:\users\*\appdata\local\programs\python\python311\lib\site-packages\pandas\*
Proceed (Y/n)? Y
  Successfully uninstalled pandas-2.0.2

You can run any pip command , not just installing or uninstalling libraries, for example to list all the installed libraries.

import pip

pip.main(['freeze'])

To check whether there are broken dependencies:

import pip

pip.main(['check'])

As you have seen, calling pip this way raises a warning. The officially recommended way to call pip programmatically is by calling pip's command-line interface via a subprocess, as we discussed previously.