Introduction
Pip is an essential tool for managing Python packages. If you’re using a version of Python that does not recognize pip commands directly from the Command Prompt (CMD), this tutorial will guide you through setting up your environment to use pip effectively.
Prerequisites
- Ensure Python 2.7 or later is installed on your Windows machine.
- Have administrative privileges for editing system environment variables.
Understanding the Issue
When attempting to run a Pip command from CMD, such as pip install <package-name>
, you may encounter an error message indicating that pip is not recognized. This usually happens because pip’s location is not included in the system’s PATH environment variable, which helps Windows locate executables.
Step-by-Step Solution
1. Verify Python Installation Path
Firstly, confirm your Python installation path:
- Open Command Prompt.
- Type
python
and press Enter. You should see version information indicating where Python is installed (e.g.,C:\Python27\
). If not, ensure Python is correctly installed.
2. Locate Pip Executable
Pip is typically located in the Scripts
directory within your Python installation folder:
- For Python 2.x:
C:\Python27\Scripts
- For Python 3.x:
C:\Python39\Scripts
(or similar, depending on the version)
3. Update Environment Variables
Add both the Python and Scripts directories to your system’s PATH variable.
Steps for Adding Paths:
-
Access System Properties:
- Right-click on ‘This PC’ or ‘Computer’ on the desktop or in File Explorer.
- Select ‘Properties’.
- Click on ‘Advanced system settings’.
-
Environment Variables:
- In the ‘System Properties’ window, click on the ‘Environment Variables…’ button.
-
Edit PATH Variable:
- Under ‘System variables’, find and select
Path
, then click ‘Edit…’. - Add both directories (e.g.,
C:\Python27
andC:\Python27\Scripts
) by clicking ‘New’ and entering each path.
- Under ‘System variables’, find and select
-
Confirm Changes:
- Click ‘OK’ to close all dialogs.
4. Test Pip Installation
To verify that pip is now recognized:
- Open a new Command Prompt window as an administrator.
- Type
pip --version
orpython -m pip --version
. - If configured correctly, it should display the installed version of pip.
5. Running Pip Commands
With the PATH set up, you can run pip commands directly from CMD:
- Install a Package:
pip install <package-name>
- List Installed Packages:
pip list
- Upgrade Pip:
python -m pip install --upgrade pip
Using Python Launcher (py
)
For systems with multiple Python versions, consider using the Python launcher (py
) to specify which version of pip to use:
- Install a package for Python 3:
py -3 -m pip install <package-name>
- Install a package for Python 2.7:
py -2.7 -m pip install <package-name>
This approach ensures compatibility and flexibility across different Python environments.
Conclusion
By following these steps, you should be able to run pip commands seamlessly from CMD on your Windows machine. Always ensure your environment variables are correctly set up whenever adding new software paths or upgrading existing ones. This practice is vital for maintaining a functional development setup.