Keywords: Python 2.7 | pip | Linux Environment Configuration
Abstract: This article provides a comprehensive guide on how to properly use Python 2.7's pip tool in CentOS and other Linux systems, addressing the issue where default pip points to Python 2.6. The article first analyzes the root cause of the problem, then presents two main solutions: direct usage of pip2.7 command and invocation through python2.7 -m pip module. Each method includes detailed installation steps, verification processes, and practical usage examples to help developers quickly switch between Python version environments.
Problem Background Analysis
When multiple Python versions are installed on Linux systems, pip version conflicts often occur. After installing Python 2.7, users may find that executing the pip -V command still shows connection to Python 2.6:
pip 1.3.1 from /usr/lib/python2.6/site-packages (python 2.6)
while the expected Python 2.7 version pip is located in the /usr/local/lib/python2.7/site-packages directory. This situation typically occurs in environments where Python 2.6 was pre-installed and Python 2.7 was installed later.
Solution 1: Using pip2.7 Command
This is the most direct and effective solution, using the dedicated pip2.7 command to manage Python 2.7 packages.
Check if pip2.7 is Installed
First, use the which command to check if pip2.7 exists in the system:
which pip2.7
If the command returns a path similar to /usr/local/bin/pip2.7, it indicates that pip2.7 is properly installed and configured in the PATH environment variable.
Install pip2.7
If which pip2.7 returns no result, manual installation is required:
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python2.7 get-pip.py
This installation process will:
- Download the official pip installation script
- Execute installation using Python 2.7 interpreter
- Install pip2.7 to Python 2.7's site-packages directory
- Create the
/usr/local/bin/pip2.7executable file
Verify Installation Results
After installation, execute verification commands again:
which pip2.7
pip2.7 -V
This should display correct Python 2.7 version information, confirming that pip is successfully associated with the target Python version.
Solution 2: Invocation Through Python Module
As an alternative approach, pip module can be directly invoked through Python interpreter, which doesn't rely on PATH environment variable.
Using python2.7 -m pip
Use Python 2.7's pip through the following command format:
python2.7 -m pip <commands>
For example, installing Django package:
python2.7 -m pip install django
Ensure pip Module Availability
If prompted that pip module doesn't exist, use ensurepip for bootstrap installation:
python2.7 -m ensurepip
This command will bootstrap pip installation to the current Python environment, possibly requiring administrator privileges:
sudo python2.7 -m ensurepip
Practical Application Examples
Here's a complete Django project environment configuration example:
# Install Django using pip2.7
pip2.7 install django==1.11
# Or using module approach
python2.7 -m pip install django==1.11
# Verify installation
python2.7 -c "import django; print(django.get_version())"
Environment Variable Configuration Suggestions
For long-term convenience, consider the following configurations:
- Set alias in shell configuration file:
alias pip='pip2.7' - Or create symbolic link:
sudo ln -s /usr/local/bin/pip2.7 /usr/local/bin/pip - Note: Modifying default pip pointing may affect other Python scripts in the system
Conclusion
Through the two methods introduced in this article, developers can flexibly manage package dependencies in multi-Python version environments. The pip2.7 command approach is recommended as it provides the most direct version control and clear execution path. In automated scripts or continuous integration environments, the module invocation approach offers better portability.