Python
pip
upgrade error
import error
troubleshooting

Error after upgrading pip cannot import name 'main'

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you have ever upgraded pip and then found yourself staring at an ImportError: cannot import name 'main' error every time you try to install a package, you are not alone. This is one of the most common Python environment issues, and it happens because pip version 10 and later moved its internal entry point to a different location. The good news is that the fix is straightforward once you understand why the error occurs.

Why This Error Happens

Prior to pip version 10, the main() function lived directly in the pip module. The system-level pip script (typically located at /usr/bin/pip or /usr/local/bin/pip) was a thin wrapper that imported and called this function:

python
# Old /usr/bin/pip script (pip < 10)
from pip import main
sys.exit(main())

Starting with pip 10, the developers moved main() into pip._internal as part of a restructuring to separate public and private APIs. The new location is:

python
# pip >= 10 internal structure
from pip._internal.cli.main import main

The problem arises when you upgrade pip (especially with sudo pip install --upgrade pip) but the system pip wrapper script at /usr/bin/pip does not get updated. The old script still tries to import main from the top-level pip module, which no longer exports it.

The Error Traceback

The error typically looks like this:

 
1Traceback (most recent call last):
2  File "/usr/bin/pip", line 9, in <module>
3    from pip import main
4ImportError: cannot import name 'main'

Or on some systems:

 
1Traceback (most recent call last):
2  File "/usr/bin/pip3", line 9, in <module>
3    from pip import main
4ImportError: cannot import name 'main' from 'pip' (/usr/lib/python3/dist-packages/pip/__init__.py)

The key detail is the file path in line 1. It points to the system pip script that was written for an older version of pip and has not been updated to match the new internal structure.

Fix 1: Use python -m pip

The simplest and most reliable fix is to stop using the pip command directly and instead invoke pip through the Python interpreter:

bash
1# Instead of:
2pip install requests
3
4# Use:
5python -m pip install requests
6
7# Or for Python 3 specifically:
8python3 -m pip install requests

This bypasses the broken wrapper script entirely. The -m flag tells Python to run pip as a module, which always uses the correct entry point for the installed version. You can create a shell alias to make this your default:

bash
# Add to ~/.bashrc or ~/.zshrc
alias pip='python3 -m pip'
alias pip3='python3 -m pip'

Fix 2: Repair the pip Script

If you want the pip command to work directly, you can fix the wrapper script. First, find where it is located:

bash
which pip
# /usr/bin/pip  or  /usr/local/bin/pip

Then edit the script to use the new import path:

python
1#!/usr/bin/python3
2import sys
3from pip._internal.cli.main import main
4
5if __name__ == '__main__':
6    sys.exit(main())

On Linux systems, you will need sudo to edit files in /usr/bin/. Be careful to use the correct Python interpreter path in the shebang line.

Fix 3: Reinstall pip with get-pip.py

If the above fixes do not resolve the issue or your pip installation is thoroughly broken, you can reinstall pip from scratch using the official bootstrap script:

bash
1# Download get-pip.py
2curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
3
4# Install pip for your Python version
5python3 get-pip.py
6
7# Verify the installation
8python3 -m pip --version

This downloads a fresh copy of pip and installs it correctly, including a properly configured wrapper script. If you are on a system where the old pip was installed with sudo, you may need to run the install with sudo as well, though using a virtual environment is strongly preferred.

Fix 4: Reinstall the System Package (Debian/Ubuntu)

On Debian-based systems, the pip command is managed by the python3-pip package. You can force-reinstall it to restore the correct wrapper:

bash
1sudo apt-get remove python3-pip
2sudo apt-get install python3-pip
3
4# Or force reinstall
5sudo apt-get install --reinstall python3-pip

This restores the distribution-provided pip script, which is configured to work with the version of pip packaged for your OS.

Prevention: Use Virtual Environments

The root cause of this error is almost always running sudo pip install --upgrade pip on a system-managed Python installation. The upgraded pip replaces the system package but cannot update the system wrapper script. The best prevention is to never modify the system Python environment directly:

bash
1# Create a virtual environment
2python3 -m venv myproject
3
4# Activate it
5source myproject/bin/activate
6
7# Now pip install freely, no sudo needed
8pip install --upgrade pip
9pip install requests flask numpy

Inside a virtual environment, pip manages its own wrapper scripts in the bin/ directory, so upgrades work correctly. The system Python and its packages remain untouched.

bash
1# Alternative: use pipx for CLI tools
2python3 -m pip install --user pipx
3pipx install black
4pipx install httpie

For installing Python-based command-line tools globally, pipx creates isolated environments automatically and avoids conflicts with the system Python entirely.

Verifying Your Fix

After applying any of the fixes above, verify that pip is working:

bash
1python3 -m pip --version
2# pip 24.0 from /home/user/.local/lib/python3.11/site-packages/pip (python 3.11)
3
4python3 -m pip install --upgrade pip
5# Successfully installed pip-24.0
6
7pip --version
8# Should now work without the ImportError

If pip --version still fails but python3 -m pip --version works, the wrapper script is still broken. Either fix it manually or rely on the python -m pip approach with the shell alias.

Common Pitfalls

  • Using sudo pip install on system Python is the primary cause of this error. System Python should be managed by your OS package manager, not pip directly.
  • Mixing pip and pip3 on systems with both Python 2 and Python 3 can cause confusion because they may point to different interpreters. Always verify which Python your pip is linked to with pip --version.
  • Forgetting to activate the virtual environment before running pip install falls back to the system pip, which may be broken.
  • Editing the wrong pip script when multiple Python installations exist leads to wasted effort. Check the traceback carefully for the exact file path.
  • Upgrading pip inside a Docker container without rebuilding the image causes a broken pip to persist across containers. Fix the Dockerfile rather than patching running containers.

Summary

  • The cannot import name 'main' error occurs because pip 10+ moved main() from pip to pip._internal, but the system wrapper script still references the old location.
  • The quickest fix is using python3 -m pip instead of the pip command, which bypasses the wrapper entirely.
  • For a permanent fix, you can repair the wrapper script, reinstall pip with get-pip.py, or reinstall the system python3-pip package.
  • Prevent this problem entirely by using virtual environments and never running sudo pip install on your system Python.
  • Always verify your fix with python3 -m pip --version to confirm pip is functioning correctly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.