In this article, we will learn how to fix the error: Failed building wheel for X in Python while creating wheel.
In CodersPacket, I always give the reason behind an error and then show how to fix that.
Why do we get “Failed building wheel for X”
When pip is unable to locate a pre-built wheel for a specified requirement, it proceeds to download the source distribution and attempts to create a wheel on the local machine. If this process is successful, the newly built wheel is saved in pip’s cache to facilitate quicker reinstalls in the future. However, if the wheel creation fails, pip reverts to the traditional method of installing from the source distribution by executing the command python setup.py install
.
Even if you are trying to create your own package, you can find this error while building a wheel.
How to fix
Hey there, pip maintainer here with some good news!
Heads up: Starting with Python 3.12, you won’t need to worry about this anymore. Pip will smartly handle isolated builds in new virtual environments, all by itself.
Here’s a quick tip for now:
Just run pip install wheel
in each new virtual environment you set up with venv. Easy peasy!
Want to know why? Here’s the scoop:
When you come across a package that’s not in wheel format, pip does its best to build one using setup.py bdist_wheel
. But sometimes things don’t go as planned – maybe there’s a missing library on your system, or some other hiccup – and you’ll see that “Failed building wheel for {…}” message.
In these cases, pip might still manage to install your package using the old-school setup.py install
method. But hey, we try to use wheels whenever possible. Why? They’re just better – faster installs, you can cache them, and they don’t need to run the code again, not to mention they’re the standard now.
The error you’re seeing is likely because the wheel package, which is essential for building wheels, isn’t there. Don’t worry, running pip install wheel
should fix it. But remember, it won’t magically solve other build issues related to your system setup.
And guess what? We’re working on shifting to a cooler, more modern build system in the future (you can get a sneak peek if you’re a package author by adding a pyproject.toml
). This new system will use isolated build environments and will have wheel installed by default.
What I did to fix Failed building wheel for X
Yesterday, I encountered an issue while installing ‘hddfancontrol’ using pip3. The command pip3 install hddfancontrol
resulted in a “Failed building wheel for hddfancontrol” error, followed by a “Failed to build hddfancontrol” message. The root of the problem turned out to be an “invalid command ‘bdist_wheel'”, as indicated by the error message “Running setup.py bdist_wheel for hddfancontrol … error”. Fortunately, I was able to resolve this issue simply by executing pip3 install wheel
.
Solve by disabling the cached copy
I’ve found a handy solution. Usually, I bypass the cached copy by using the command: pip install <package> --no-cache-dir
.
But note that, this is just a way to get rid of the error message.