Python `no module pip.__main__;` error when trying to install a module

Question:

I am getting the following error on my Raspberry Pi: No module named pip__main__; 'pip' is a package and cannot be directly executed

When I type in to the terminal: sudo python3 -m pip install mp3play

What is causing this and how can I fix it so that I can install the module mp3play?

Asked By: TheHarpoon

||

Answers:

pip is a standalone executable. If pip if in your path, you can just execute

pip install mp3play

If pip is not in your path, then you need to navigate to the directory where pip is located and then execute the above.

If needed, add sudo to the command.

The precise error you are encountered is due to pip being a package, but -m is used for executing modules.

EDIT: pip also comes with several helpful alias functions that point to different Python installs. In general, pip points to your main Python install (the one you enter when simply executing python), pipV where V is a number such as 2 or 3 adds the install to your primary Python of version V (pip3 adds to your python3 environment). Finally there is pipV.S where V is the same as before and S is the subversion. For instance pip3.4 installs for Python 3.4.

Answered By: BobChao87

Pip is not only a standalone executable, it is also a python module.

In fact in the python docs it directly recommends using the -m syntax for installing a package using pip.

See https://docs.python.org/3.5/installing/index.html#basic-usage:

The standard packaging tools are all designed to be used from the command line.

The following command will install the latest version of a module and its dependencies from the Python Packaging Index:

python -m pip install SomePackage

My guess would have been that your system’s pip (the executable) was being shadowed by the python2 version of the pip executable. But it sounds like you don’t have pip (the module) installed such that your python3 executable can find it, so you may need to reinstall pip (the module) specifically.

For that use python3 -m ensurepip (docs for ensurepip) which will install pip if it is not present from the persepctive of your python3 interpreter.

The other issue could be that it’s finding a file, executable or directory called pip in your current directory, and it is trying to treat that pip as a module, and it is not in fact a module.

If it’s not that I’m not sure. But it is definitely not because pip is not a module.

Answered By: mpacer

I had the same problem. I found that an old pip directory was left over from a python 2.7 install, at C:Usersmy-usernamepip. This was causing python to try to load pip from there and fail.

I removed that directory and my error has just become No module named pip.

I haven’t solved the problem yet, but I’m working through it at http://bugs.python.org/issue29586

Answered By: FusterCluck

I had the same issue and none of the previous answers solved it for me.

The error appeared when I uninstalled and reinstalled python to my PC. As it appears the previous existing version of pip wasn’t completely removed and when I was trying to import it with python -m pip install package it was actually trying to call the previous version.

To solve it first manually delete the pip folders in the following locations:

C:Usersusernamepip
C:UsersusernameAppDataLocalpip
C:UsersusernameAppDataLocalProgramsPythonPython**libsite-packagespip***
C:Python**pip

Then download get-pip.py.

Finally, navigate to the folder where you downloaded it and run:

python get-pip.py

This procedure should reinstall pip and fix the issue.

Answered By: Djib2011

Explanation:

This can occur when the existing pip module may get affected while upgrading it.

You might observe in site-packages, that any required file/ folder has been renamed with a leading ~, say pip changed to ~ip or the subfolders with a leading ~.

So, while referring to the module pip, if fails to find the required executable files.

The reason this happens is read:

When uninstalling a package, pip will now rename it in place to a name
that cannot be imported, and once it has confirmed that everything
will succeed (including installing new versions if it’s doing an
upgrade), only then will it delete those folders. If something fails,
it renames them back.

Solution:

In addition to the solution by @Djib2011, alternatively you can use to fresh install the pip module:

py <-version> -m ensurepip --upgrade

where <-version> is an optional argument and can be replaced with a version of python you have been facing issues with say, -3.8.

Python comes with an ensurepip module, which can install pip in a Python environment. This would install the pip module which was initially packaged with the version of python you have been using.

Post which you can run the upgrade command to port pip to the newer version.

py <-version> -m pip install --upgrade pip

You can append the --user option if you face any EnvironmentError due to permission.

Reference:

  1. pip installation
  2. pip how to remove incorrectly installed package with a leading dash
Answered By: Shubham Srivastava

Here’s my take on this:

No module named install.__main__; 'install' is a package and cannot be directly executed

Happens when I follow the above instructions in this thread, as well as other instructions.

I have used exactly the same command as the author…..

I don’t understand why its not working for me….

I will be deleting my account as of being blocked from answering questions people may have if no one found this helpful.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.