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.
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:
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:
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:
Or on some systems:
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:
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:
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:
Then edit the script to use the new import path:
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:
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:
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:
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.
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:
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 installon system Python is the primary cause of this error. System Python should be managed by your OS package manager, not pip directly. - Mixing
pipandpip3on 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 withpip --version. - Forgetting to activate the virtual environment before running
pip installfalls 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+ movedmain()frompiptopip._internal, but the system wrapper script still references the old location. - The quickest fix is using
python3 -m pipinstead of thepipcommand, which bypasses the wrapper entirely. - For a permanent fix, you can repair the wrapper script, reinstall pip with
get-pip.py, or reinstall the systempython3-pippackage. - Prevent this problem entirely by using virtual environments and never running
sudo pip installon your system Python. - Always verify your fix with
python3 -m pip --versionto confirm pip is functioning correctly.
Related reading
- ERROR Cannot uninstall 'wrapt'. when installing tensorflow-gpu1.14
- ERROR Could not build wheels for scipy which use PEP 517 and cannot be installed directly
- ERROR Could not find a version that satisfies the requirement tensorflow from versions none ERROR No matching distribution found for tensorflow
- ERROR Could not install packages due to an OSError WinError 5
- ERROR Android emulator gets killed in Android Studio
- Error ANDROID_HOME is not set and android command not in your PATH. You must fulfill at least one of these conditions.
- Error 'DataFrame' object has no attribute 'append
- Error 'dict' object has no attribute 'iteritems
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.