// Tutorial //

How to Fix Python `No such file or directory` Compiler Errors When Installing Packages

Published on October 4, 2022
Default avatar
By Alex Garnett
Senior DevOps Technical Writer
How to Fix Python `No such file or directory` Compiler Errors When Installing Packages

Introduction

A common error that you may receive when installing Python modules is the No such file or directory error. This can be misleading, because you usually aren’t missing a file or directory from the package you’re trying to install. Instead, this error results from Python trying to call your system compiler during module installation. This is because the paths to your system compiler are often hardcoded into Python itself, and it is not finding the compiler files it needs. This tutorial will provide an example of this error, and the steps to fix it on multiple platforms.

Missing Compiler Errors

Python packages are typically installed using the pip package manager with the pip install command. pip will print a list of dependencies additionally required by the package you selected, and a long list of output from the install process. Sometimes, the installer will exit with an error, containing text like the following near the end of the output:

Output
x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.10 -I/usr/local/lib/python3.10/dist-packages/numpy/core/include -I/usr/include/python3.10 -c radiomics/src/_cmatrices.c -o build/temp.linux-x86_64-3.10/radiomics/src/_cmatrices.o error: command 'x86_64-linux-gnu-gcc' failed: No such file or directory [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure × Encountered error while trying to install package. ╰─> pyradiomics note: This is an issue with the package mentioned above, not pip. hint: See above for output from the failure.

This particular error was the result of trying to install pd-dwi, a Python library used in chemotherapy research, using pip install pd-dwi.

Some Python libraries, especially those used for scientific computing, need to compile additional code locally on your machine after being installed. Python is a high-level, interpreted language that can run with only the Python interpreter itself present. Low-level languages like C or Rust, which Python libraries occasionally include for high-performance processing, need to be compiled and optimized before being executable. If there isn’t a compiler present on your system, the installation will fail.

On most modern platforms, when you install Python’s package manager, pip, it will also set up a compiler environment and the associated packages. However, there are a few reasons why this may not always happen. For example, it’s possible that the compiler could have been uninstalled by accident, or never installed in the first place. And unlike on Linux, Python packages usually aren’t installed by the system package manager on Mac or Windows, creating more opportunities for trouble.

The following steps of this tutorial will provide instructions for installing and verifying a Python-compatible compiler on Ubuntu/Debian Linux, on Red Hat/Rocky Linux, on Windows, and on macOS.

Compiler Packages for Ubuntu and Debian

On Ubuntu, you can install a package called build-essential that will provide all the packages needed for a modern, well-supported compiler environment. build-essential is what’s known as a meta-package. It doesn’t refer to any one package, but rather pulls in a number of common compiler tools as dependencies.

You can also install libpython3-dev. This is a longstanding Ubuntu/Debian ecosystem package that essentially “wires up” the compiler to Python, and provides all the needed backend configuration for calling your compiler automatically from Python or from pip. Usually, it is installed automatically along with pip, but if you install pip without using the package manager, you can miss it.

Install the packages with apt:

  1. sudo apt install build-essential libpython3-dev

This will also install a number of dependencies. Afterward, you can verify that a compiler is available by checking for the existence of the make command on your system. To do that, use the which command:

  1. which make
Output
/usr/bin/make

make is the command that gcc, the most popular open-source compiler, uses to parse a Makefile, which is how compile instructions are provided within each package. If you now have a version of make installed on your path, try installing your Python module using pip again.

Compiler Packages for Red Hat and Rocky Linux

On Red Hat and Rocky Linux, you can use the groups functionality of the dnf package manager to install a group of packages that include a well-supported compiler environment. The group of packages you’ll install is called "Development Tools".

Use two dnf commands to install the package group:

  1. sudo dnf groups mark install "Development Tools"
  2. sudo dnf groupinstall "Development Tools"

This will also install a number of dependencies. Next, you can install python3-devel, a longstanding Red Hat ecosystem package that essentially “wires up” the compiler to Python. python3-devel provides all the needed backend configuration for calling your compiler automatically from Python or from pip:

  1. sudo dnf install python3-devel

Afterward, you can verify that a compiler is available by checking for the existence of the make command on your system. To do that, use the which command:

  1. which make
Output
/usr/bin/make

make is the command that gcc, the most popular open-source compiler, uses to parse a Makefile, which is how compile instructions are provided within each package. If you now have a version of make installed on your path, try installing your Python module using pip again.

Windows Compiler Environments

Windows compiler issues can be trickier, because there are many different ways of installing Python, and each of them expect different compilers to be present:

  • If you use Python with WSL2, it’s just like running Python under Linux, so you can follow the troubleshooting instructions for your distro (Ubuntu by default).

  • If you use Python with Anaconda, it will provide its own compiler packages within the conda environment, usually avoiding any of these errors in the first place.

  • If you use Python on Windows natively, there are a few other considerations. By default, Python on Windows tries to use the Microsoft Visual Studio Build Tools. This is a very large install and adds many Windows ecosystem packages that may be unfamiliar if you mostly work in the cloud, but should work automatically after installation, like installing make on Linux.

  • If you already have a working version of open-source gcc and make build tooling installed in your Windows environment using MinGW or Chocolatey, you can tell Python to use this compiler on Windows instead, by creating a file at Lib/distutils/distutils.cfg relative to your Python install path and adding the following contents:

Lib/distutils/distutils.cfg
[build]
compiler=mingw32

[build_ext]
compiler=mingw32

If you have trouble installing a compiler on Windows, you can try to install a precompiled wheel package for the library you are installing instead, though this is less convenient than installing from pip and they are usually only available on an ad-hoc basis.

macOS Compiler Environments

macOS bundles its compiler toolchain into Apple’s development suite, XCode. Like Visual Studio on Windows, XCode is a complete development environment with its own interface, but you won’t actually need to use XCode itself to compile Python packages. Instead, you only need to make sure that the XCode packages themselves are installed. You can do this by running xcode-select –install:

  1. xcode-select --install

You’ll be prompted to start the installation, and then prompted again to accept a software license. Then the tools will download and install automatically.

Conclusion

The Python ecosystem is very powerful, and welcoming to both novice and expert developers, but encountering gaps in its tooling can be confusing. In this tutorial, you learned how to fix errors that may arise as a result of missing compiler packages, and when Python needs to compile low-level code at module installation time.

Next, you may want to review our How to Code in Python series.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Senior DevOps Technical Writer

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!