Problem Statement: How to deal with error after upgrading pip: cannot import name’main’?
pip is a package management tool in Python that allows us to install and manage external Python libraries and dependencies that are not a part of the standard Python library. In other words, it is a utility that allows us to manage PyPI package installations from the command line.
- pip is an acronym for “Pip Installs Packages” or “Pip Installs Python“. Alternatively, it also stands for “preferred installer program”.
- The Good News: Python 2.7. 9 and later (on the Python2 versions), and Python 3.4 and later (on Python3 versions) are shipped with pip by default. This allows newbies to access the community libraries without having to face the difficulty of setup.
Recommended Read: How To Install pip On Windows?
That was a lil preview about pip. Now let us dive into our mission-critical question and understand how the error after upgrading pip: cannot import name ‘main’ occur.
- This error mostly occurs when you have accidentally upgraded the pip in our system.
- It appears when
unpack_urlare used to download packages from
PyPi. In this case, you have to just reinstall the packages.
Example: Here’s a question taken from StackOverflow that highlights the problem of your discussion here.
pip3 command that appears in the error above is provided by the package maintainer. It seems to be a file that is not managed by system pip in this case.
Here’s a word of advice for you to avoid this error – “Do not try to modify the package-manager managed files (in this case the user tried to play around with the system pip installation). Instead, you should use a virtualenv.” The bottom line is – It is a very bad idea to replace the system pip.
If you still want to get away with this you can probably try the following command (considering that you are using Python 3.x) –
python3 -m pip install package_name
In case you are using Python 2, use this command instead:
python -m pip install package_name.
Note: You need to ensure which interpreter is used on your pc. On most of the operating systems, pip generally uses the Python version 2 interpreter, whereas
pip3 uses the Python version 3 interpreter. If you have only installed Python 3 on your pc, pip is identical to
pip3. To know which interpreter is being used you have to read the first line of the file pip.
You can opt to resolve this issue by simply recovering the original
pip binaries. Considering that you are on Ubuntu use the following command –
python -m pip uninstall pip && sudo apt install python-pip --reinstall
python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
A Side Note (Only for Ubuntu Users)
The Ubuntu users can solve this method by using the following command to uninstall the pip 10 and retain your Ubuntu provided patched pip 8:
$ python -m pip uninstall pip
Always ensure to use apt wherever possible for a system-wide installation of modules unless you are using a virtual environment. In the older versions of Ubuntu or Debian, you must always add –user flag when you are using pip outside of the virtual environments.
Sometimes the operating system allows you to install common Python modules, including numpy quickly with APT, without the need for pip, for example:
# With system dependencies: $ sudo apt install python3-numpy python3-scipy $ sudo apt install python3-pip (older version) # Apt Syntax: To resync the package index files from up to date sources: $ sudo apt update # To perform a text search on all the available packages: $ apt search <python-package-name> # To display all the detailed package description: $ apt show <python-package-name> $ sudo apt install <python-package-name>
However, if you want to use the pip 10 exclusively, there are the following methods to do it:
You have to re-open a bash session like a new terminal tab, or type bash. Following this, pip 10 becomes available. However, Debian’s pip 8 remains installed in your os but remains broken. Thus, you have to refresh pip pathname in the $PATH by using the following command:
$ hash -d pip && pip -V
Since Debian’s pip 8 remains installed in your os in a broken state, you have to use the following command to uninstall Debian’s pip 8 completely, to make space for your new pip 10.
$ sudo apt remove python-pip && hash -d pip (for Python 3 versions it's python3 - pip)
Note: Always ensure that you have to add the –user flag to the non-Debian provided pip 10 unless you are in a virtual environment. Ubuntu or Debian does not support pip 10 system-wide outside the virtual environment and hence it could lead to an error.
A Quick Recap to Import Statement and ImportError
In Python, the
import statement serves two main purposes:
- It is used to search for the module specified by its name, then load and initialize it if the module is required.
- It additionally defines a name in the local namespace within the scope of the import statement. This local name would then be able to be used to reference the accessed module throughout the whole code.
If an import statement experiences difficulty in successfully importing a module, it raises an ImportError. Commonly, such an issue occurs because of a faulty installation or an invalid path, which will usually raise a ModuleNotFoundError in Python 3.6 and newer versions.
The following video shows you how to resolve the
I hope this tutorial answers your queries. Please stay tuned and subscribe for more solutions and interesting discussions in the future. Happy coding!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.