Python is one of the most popular programming languages at the time, mostly because of its ease of use and the availability of hundreds of thousands of libraries and APIs that further extend the functionality of the language.
However, as simple as writing code has become, it still throws challenges at developers, often uncalled-for bugs and glitches. In this article, we’re talking about the “error: can’t find Rust compiler” error in Python, its causes and what you can do to fix the problem.
What causes this error?
The error mostly happens when installing the cryptography library, which requires Rust during installation. Specifically, versions higher than 3.5 require Rust to be installed. Any version before this allows users to skip the Rust requirement and build the library regardless.
How to fix this?
Here are three fixes you can try out.
The simplest fix to the problem is to install Rust. You can skip the installation, as detailed in the solution given below, but that can set you up for future issues as you’ll have to use version 3.4 or lower.
You don’t have to do anything special to install Rust, modify your RUN apk add command to the one given below and you should be good to go.
FROM python:3.7-alpine ENV PYTHONUNBUFFERED 1 RUN apk add --update \ build-base \ cairo \ cairo-dev \ cargo \ freetype-dev \ gcc \ gdk-pixbuf-dev \ gettext \ jpeg-dev \ lcms2-dev \ libffi-dev \ musl-dev \ openjpeg-dev \ openssl-dev \ pango-dev \ poppler-utils \ postgresql-client \ postgresql-dev \ py-cffi \ python3-dev \ rust \ tcl-dev \ tiff-dev \ tk-dev \ zlib-dev RUN pip install cryptography
Do keep in mind that we’re assuming you’re using version 3.7-alpine. If you’re using another Python version, modify the command accordingly.
Also read: How to fix error: getaddrinfo enotfound?
Skip the Rust installation
If you’re using cryptography version 3.4 or lower, you can skip the Rust requirement by adding the following line before adding commands for the build.
Alternatively, you can add this line to your pyvenv.cfg file if you’re running virtual environments. After that, you can install cryptography using Pip as follows.
pip install cryptography
Some users have reported that simply updating Pip resolved their issue. Try running the following command to update Pip.
pip install -U pip
Upgrading to Pip version 19.1,1 or higher can fix the problem as newer versions automatically install any precompiled packages, hence reducing the number of manual dependency installs users have to make.