241

This is a truly popular question here at SO, but none of the many answers I have looked at, clearly explain what this error really mean, and why it occurs.

One source of confusion, is that when (for example) you do pip install pycparser, you first get the error:

Failed building wheel for pycparser

which is then followed by the message that the package was:

Successfully installed pycparser-2.19.


# pip3 install pycparser

Collecting pycparser
  Using cached https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz
Building wheels for collected packages: pycparser
  Running setup.py bdist_wheel for pycparser ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-g_v28hpp/pycparser/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-__w_f6p0 --python-tag cp36:
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    ...
    File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2349, in resolve
      module = __import__(self.module_name, fromlist=['__name__'], level=0)
  ModuleNotFoundError: No module named 'wheel.bdist_wheel'

  ----------------------------------------
  Failed building wheel for pycparser
  Running setup.py clean for pycparser
Failed to build pycparser
Installing collected packages: pycparser
  Running setup.py install for pycparser ... done
Successfully installed pycparser-2.19

What is going on here?

(I would like to understand how something can fail but still get installed and whether you can trust this package functioning correctly?)

So far the best partial explanation I have found is this.

randers
  • 5,031
  • 5
  • 37
  • 64
not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • 18
    When `pip` doesn't find a wheel for the requirement, it downloads the source dist and tries to build a wheel from it locally. on success, the wheel is stored in `pip`'s cache for future reinstalls. on wheel build failure, `pip` switches to the legacy installation from source dist (invoking `python setup.py install`). – hoefling Nov 08 '18 at 09:49
  • 1
    In your case, you're missing the `wheel` package so `pip` is unable to build wheels from source dists. if you want to explicitly disable building wheels, use the `--no-binary` flag: `pip install somepkg --no-binary=somepkg`. Or use `pip install somepkg --no-binary=:all:`, but beware that this will disable wheels for every package selected for installation, including dependencies; if there is no source dist available for some package `pip` needs to install, the installation will fail. – hoefling Nov 08 '18 at 09:54
  • 3
    @hoefling: your first comment was the true reason and could be an answer. The second one is wrong: `--no-binary` instructs pip to only *download* and use source distributions. The flag to prevent it to build a local binary wheel is indeed `--no-cache-dir`. – Serge Ballesta Nov 08 '18 at 11:03
  • @hoefling I have `wheels` (0.32.2) so that is not the problem. But maybe the `pycparser` package doesn't have a wheel (`*.whl`) associated? But how can I check this *a-priori*? – not2qubit Nov 08 '18 at 11:18
  • 1
    You can consult the PyPI site at https://pypi.org/project/pycparser/ and then asks for the [files](https://pypi.org/project/pycparser/#files). You can then see that only a `.tar.gz` file is there and it is the source distribution on PyPI (a wheel would have a `.whl` extension) – Serge Ballesta Nov 08 '18 at 12:23
  • @SergeBallesta Example: `pip install docopt --no-binary=docopt`, the output clearly states: `Skipping bdist_wheel for docopt, due to binaries being disabled for it.` When turning the verbose mode on, it's clear that the installation is done via `setup.py install`. [the relevant spot in `pip`'s sources](https://github.com/pypa/pip/blob/master/src/pip/_internal/wheel.py#L777). – hoefling Nov 08 '18 at 12:36
  • @SergeBallesta however, i didn't expect `--no-cache-dir` to implicitly disable wheel building; kinda suprprising to learn that. Always thought `--no-cache-dir` only ignores the local cache dir (for both reading and writing), so one can use that for rebuilding the local wheel instead of taking the cached one. Learned something new today! – hoefling Nov 08 '18 at 12:41
  • I found a solution by looking carefully at the error messages and it was written `python-src/curve25519/curve25519module.c:3:10: fatal error: Python.h: No such file or directory`. In my case the problem was fixed by installing `python-dev`: https://stackoverflow.com/questions/54686540/fatal-error-python-h-no-such-file-or-directory-but-python-dev-is-already-inst – baptx Jun 29 '21 at 16:04
  • Where are the logs located, typically? – Edmondo Sep 17 '21 at 18:47
  • @Edmondo1984 AFAIK, there are no default installation logs for python, unless you [add one yourself](https://stackoverflow.com/questions/53844927/where-are-pip-installation-logs) in a default **pip.ini** (win) or **pip.conf**, or use something like `python setup.py install >& pip.log`. – not2qubit Sep 18 '21 at 16:17

17 Answers17

208

(pip maintainer here!)

For a quick copy paste:

pip install wheel

Do that in every new virtual environment created with venv.

Read on for the details and explaination.


If the package is not a wheel, pip tries to build a wheel for it (via setup.py bdist_wheel). If that fails for any reason (like, missing system level libraries, incompatibilities with your system, bad version string in the built wheel, etc), you get the "Failed building wheel for {...}" message.

In some of these cases, currently, pip falls back to installing via setup.py install, so it's possible that the installation still succeeds. That said, pip always tries to install packages via wheels as often as it can. This is because of various advantages of using wheels (like faster installs, cache-able, not executing code again etc) and the fact that it is a standardizd format; unlike the (deprecated) setup.py install interface.


Your error message here is due to the wheel package being missing, which contains the logic required to build the wheels in setup.py bdist_wheel. (pip install wheel can fix that -- but it won't fix any build time issues due to system configuration)


Sometime in the future, we'll switch to a more modern build system by default (if you're a package author, you can opt-in by adding a pyproject.toml) that will solve this issue, through isolated build environments where you will have wheel installed. :)

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
  • 1
    At the time of writing OP, I already had the `wheel` package installed. Also please add a link to PEP517. – not2qubit Jun 08 '19 at 15:17
  • Did you have an older version of setuptools or wheel installed? If not, that seems like the reason for the failure there. Anyway, from pip's POV, it failed to build the wheel and thus it installs normally. – pradyunsg Aug 28 '19 at 15:02
  • Is there a way to skip wheel generation (`setup.py bdist_wheel`) and just do the installation (`setup.py install`)? – K3---rnc Nov 06 '20 at 03:11
  • Also make sure that the wheel package is installed in the virtual environment that you are operating in (not just the machine). – PiperWarrior Mar 05 '21 at 22:37
  • 1
    @K3---rnc None that aren't already deprecated. – pradyunsg Apr 30 '21 at 20:58
  • We're now well into 2022 and this is still an issue. pip install wheel did "stuff" but on trying finish my original installs related to creating a Python virtual environment, they still fail. I had to persevere with a Windows-based setup. What an absolute waste of time. – AndyUK Apr 05 '22 at 08:00
  • @AndyUK Thanks for that rambling -- It flagged to me that my answer wasn't going into the nuance of a system that is misconfigured for being able to use/build a certain package. :) – pradyunsg Apr 07 '22 at 07:04
72

Yesterday, I got the same error: Failed building wheel for hddfancontrol when I ran pip3 install hddfancontrol. The result was Failed to build hddfancontrol. The cause was error: invalid command 'bdist_wheel' and Running setup.py bdist_wheel for hddfancontrol ... error. The error was fixed by running the following:

 pip3 install wheel

(From here.)

Alternatively, the "wheel" can be downloaded directly from here. When downloaded, it can be installed by running the following:

pip3 install "/the/file_path/to/wheel-0.32.3-py2.py3-none-any.whl"
not2qubit
  • 14,531
  • 8
  • 95
  • 135
Matthew Wai
  • 962
  • 7
  • 14
55

Since, nobody seem to mention this apart myself. My own solution to the above problem is most often to make sure to disable the cached copy by using: pip install <package> --no-cache-dir.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • 3
    If just getting rid of the message is your goal, this may count as a fix, otherwise I'd call this a workaround. – Nils Magnus Jul 22 '19 at 13:36
  • @NilsMagnus This is **not** just *`getting rid of the message`*, it is allowing the install to proceed even when the installer thinks that it has already been updated, due to the cache directory containing residue from previous and often failed updates. – not2qubit Jul 23 '19 at 19:19
  • ^^ *That* said, I keep on wondering why this is not the default behavior? Who want to keep cached copies when you're updating? Just doesn't make any sense. – not2qubit Jul 23 '19 at 19:21
  • Because bandwidth on PyPI isn't "free" and caching built wheels allows for much faster installs later. – pradyunsg Aug 28 '19 at 15:20
  • 16
    How is that the accepted answer to the question? It does not answer the question. – PascalIv Jan 15 '20 at 12:41
  • 3
    Because he himself asked it and answered it as well. I think this way he can accept his own answer I believe. Sounds weird but that might be the case. – Amit Amola Apr 02 '20 at 09:37
  • @PascalIv Because that error (used to) pop up, also under other different circumstances. Then I found an answer that worked for me and naturally accepted that one. As you can see there are many different solutions that worked for other people, but not for me. Perhaps not a fool-proof way to accepted answers, but that's just the flexibility we have to live with... – not2qubit Apr 02 '20 at 17:51
13

In my case, update the pip versión after create the venv, this update pip from 9.0.1 to 20.3.1

python3 -m venv env/python
source env/python/bin/activate
pip3 install pip --upgrade

But, the message was...

Using legacy 'setup.py install' for django-avatar, since package 'wheel' is not installed.

Then, I install wheel package after update pip

python3 -m venv env/python
source env/python/bin/activate
pip3 install --upgrade pip
pip3 install wheel

And the message was...

Building wheel for django-avatar (setup.py): started
default:   Building wheel for django-avatar (setup.py): finished with status 'done'
Jantones
  • 151
  • 1
  • 3
6

It might be helpful to address this question from a package deployment perspective.

There are many tutorials out there that explain how to publish a package to PyPi. Below are a couple I have used;

medium
real python

My experience is that most of these tutorials only have you use the .tar of the source, not a wheel. Thus, when installing packages created using these tutorials, I've received the "Failed to build wheel" error.

I later found the link on PyPi to the Python Software Foundation's docs PSF Docs. I discovered that their setup and build process is slightly different, and does indeed included building a wheel file.

After using the officially documented method, I no longer received the error when installing my packages.

So, the error might simply be a matter of how the developer packaged and deployed the project. None of us were born knowing how to use PyPi, and if they happened upon the wrong tutorial -- well, you can fill in the blanks.

I'm sure that is not the only reason for the error, but I'm willing to bet that is a major reason for it.

SteveJ
  • 3,034
  • 2
  • 27
  • 47
4

This error mostly comes up when you do not have the required packages needed by wheel. If you are using python3, then install python3-dev or python2-dev if you are using python 2.

sudo apt-get install python3-dev 

or

sudo apt-get install python2-dev
Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
demerit
  • 141
  • 1
  • 9
  • Sooner or later we would end up executing this command so better to have this executed first. Correct answer by far as there could be multiple dependencies required and this would take care of the essential. – Isaac Philip Oct 26 '22 at 17:34
  • This answer saved me, I was trying to install PandasGUI on WSL and kept getting the error "Can't build wheel for evdev", and no answers on the internet worked, but yours did! – Gabriel Caldas Apr 25 '23 at 12:50
3

Error :

System : aws ec2 instance (t2 small)

issue : while installing opencv python via

pip3 install opencv-python

  Problem with the CMake installation, aborting build. CMake executable is cmake
  
  ----------------------------------------
  Failed building wheel for opencv-python
  Running setup.py clean for opencv-python

What worked for me

pip3 install --upgrade pip setuptools wheel

After this you still might received fallowing error error

    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Installing libgl solved the error for me.

sudo apt update
sudo apt install libgl1-mesa-glx

Hope this helps

Hemant c
  • 133
  • 1
  • 5
2

Try this:

sudo apt-get install libpcap-dev libpq-dev

It has worked for me when I have installed these two.

See the link here for more information

ianrathbone
  • 2,334
  • 2
  • 15
  • 16
t w
  • 39
  • 1
2

On Ubuntu 18.04, I ran into this issue because the apt package for wheel does not include the wheel command. I think pip tries to import the wheel python package, and if that succeeds assumes that the wheel command is also available. Ubuntu breaks that assumption.

The apt python3 code package is named python3-wheel. This is installed automatically because python3-pip recommends it.

The apt python3 wheel command package is named python-wheel-common. Installing this too fixes the "failed building wheel" errors for me.

JanKanis
  • 6,346
  • 5
  • 38
  • 42
1

I got the same message when I tried to install

pip install django-imagekit. 

So I ran

pip install wheel 

(I had python 2.7) and then I reran pip install django-imagekit and it worked.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

I had the same problem while installing Brotli

ERROR

Failed building wheel for Brotli

I solved it by downloading the .whl file from here and installing it using the below command

C:\Users\{user_name}\Downloads>pip install Brotli-1.0.9-cp39-cp39-win_amd64.whl
Emma
  • 27,428
  • 11
  • 44
  • 69
Teko_boy
  • 11
  • 3
0

I stuck with this problem for several hours when I was trying to install a package that requires 'isal', but isal installation failed:

  ----------------------------------------
  ERROR: Failed building wheel for isal
Failed to build isal
ERROR: Could not build wheels for isal which use PEP 517 and cannot be installed directly

The solution that works for me is installing libtool.

yum install libtool
J.K
  • 1,178
  • 10
  • 13
0

I've had an issue as well while installing "pyblake2" package. Working on MacOS M2! Error text (approx text): "Couldn't build wheel for pyblake2..." However "wheel" module was successfully installed!

Solution:

  1. download package from the official source: https://pypi.org/project/pyblake2/#modal-close

  2. run the "setup.py" by running: ~python3 setup.py

  3. if it throws the errors:

  • a) pyblake2module.c:699:27: error: expression is not assignable Py_TYPE(&blake2bType) = &PyType_Type;
  • b) pyblake2module.c:703:27: error: expression is not assignable Py_TYPE(&blake2sType) = &PyType_Type;

Replace the lines:

  • a) Py_TYPE(&blake2bType) = &PyType_Type; -> blake2bType.tp_base = &PyType_Type;
  • b) Py_TYPE(&blake2sType) = &PyType_Type; -> blake2sType.tp_base = &PyType_Type;
  1. Repeat step #2 again.

You are welcome!

Shreyder
  • 29
  • 4
-1

I would like to add that if you only have Python3 on your system then you need to start using pip3 instead of pip.

You can install pip3 using the following command;

sudo apt install python3-pip -y

After this you can try to install the package you need with;

sudo pip3 install <package>
Maarten
  • 402
  • 5
  • 20
-3

I was trying to install python-nmap tool, and getting this error.

If you are on Linux platform, please make sure that the nmap tool is installed, otherwise the library python-nmap won't work.

On Red Hat based distribution, please install nmap CLI as follow:

sudo yum install namp
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-3

for mac-os run below command in terminal

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

-4

This may Help you ! ....

Uninstalling pycparser:

pip uninstall pycparser

Reinstall pycparser:

pip install pycparser

I got same error while installing termcolor and I fixed it by reinstalling it .