Use "pip" Package in Python Script

This section provides a tutorial example on how to install MySQL Connector/Python package with 'pip' command.

"pip" can also be used as a Python package in Python scripts to manage other Python packages. This is very useful, if you don't have command line access to the host computer.

To use "pip" in Python scripts, you need to use the following statements:

import pip
cmdline = [cmd, arg1, arg2, ...]
pip.main(cmdline)

Example 1: List Installed Packages

herong$ python 
Python 3.9.12 (main, Apr  5 2022, 06:56:58) 

>>> import pip
>>> cmdline = ['list']
>>> pip.main(cmdline)

Package                Version
---------------------- ---------
brotlipy               0.7.0
certifi                2021.10.8
cffi                   1.15.0
charset-normalizer     2.0.4
click                  8.1.3
colorama               0.4.4
conda                  4.12.0
...

Example 2: Install a Given Package

>>> cmdline = ['install', 'scrapy']
>>> pip.main(cmdline)
>>> cmdline = ['install', 'scrapy']

Collecting scrapy
  Downloading Scrapy-2.9.0-py2.py3-none-any.whl (277 kB)
...
Installing collected packages: w3lib, pyasn1, ..., scrapy

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

"pip" - Package Installer for Python

 What Is "pip/pip2/pip3" Command

Use "pip" Package in Python Script

 Install MySQL Connector/Python with "pip"

 Using "pip" with Different Sources

 Commonly Used Python Packages/Libraries

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB