Introductino to pip
pip serves as Python's package management system, providing functionality for finding, downloading, installing, and uninstalling Python packages.
To check the installed version and path:
pip --version # For Python 2.x
pip3 --version # For Python 3.x
Installing pip
Official Installation Method
Download the installation script and execute it:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
The version of pip will correspond to the Python version used to run the script. For example, using python3 get-pip.py will install pip3.
Linux Installation via apt-get
sudo apt-get install python-pip
Offline Installation
- Download the desired version from the pip download repository
- Extract the tar.gz file
- Execute
python setup.py build - Execute
python setup.py install - Verify installation with
pip --version
Managing pip Verisons
Upgrading pip
pip install -U pip
# or
sudo pip install --upgrade pip
On Windows:
python -m pip install -U pip # For Python 2.x
python -m pip install -U pip # For Python 3.x
Downgrading pip
python -m pip install pip==9.0.3
Using Multiple Python Versions
When working with both Python 2 and 3:
python2 -m pip install package # For Python 2
python3 -m pip install package # For Python 3
Common pip Commands
- Install a package:
pip install package_name - Remove a package:
pip uninstall package_name - Upgrade a package:
pip install --upgrade package_nameorpip install -U package_name - View package information:
pip show package_name - View detailed package info:
pip show --files package_nameorpip show -f package_name - List all installed packages:
pip list - Search for packages:
pip search keyword - Check for outdated packages:
pip list --outdatedorpip list -o - Export installed packages:
pip freeze > requirements.txt
To install from a requirements file:
pip install -r requirements.txt
To uninstall multiple packages:
pip uninstall -r requirements.txt
Installing Package Archives
Using pip
pip install package.tar.gz
Manual Installation
tar -xzf package.tar.gz
cd package_directory
python setup.py build
python setup.py install
Advanced pip Install Paraemters
Installing Specific Versions
pip install package==2.8.7 # Exact version
pip install package # Latest version
pip install package==1.0.4 # Specific version
pip install 'package>=1.0.4' # Minimum version
Using Domestic Mirror Repositories
Popular mirror repositories:
- http://pypi.douban.com/simple/ (Douban)
- http://mirrors.aliyun.com/pypi/simple/ (Aliyun)
- http://pypi.mirrors.ustc.edu.cn/simple/ (USTC)
- https://pypi.tuna.tsinghua.edu.cn/simple (Tsinghua)
Temporary usage with -i parameter:
pip install -i http://pypi.douban.com/simple/ flask
Setting Up a Proxy
sudo pip --proxy=http://proxy.server:port install package_name
Additional Parameters
sudo pip --default-timeout=100 install --ignore-installed --target=/custom/path package==3.0.4
--ignore-installedor-I: Reinstall even if already installed--target: Install packages to a specific directory--default-timeout: Set a custom download timeout to prevent network-related failures
Troubleshooting Common Issues
Permission Issues
Global installation:
sudo pip install package_name
User-specific installation (recommended for security):
pip install --user package_name
When encountering permission denied errors:
pip install --user package_name
This installs packages to $HOME/.local. You may need to add $HOME/.local/bin to your PATH:
export PATH=$HOME/.local/bin:$PATH
Understanding sudo -H
When using sudo, you might encounter cache warnings. The -H flag sets the home directory correctly:
sudo -H pip install package_name
This ensures the cache directory is properly owned by the current user.
Encoding Problems
When facing Unicode decode errors during installation:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
Note that in newer Python versions, you need to reload sys before setting the default encoding.