ValueError: invalid mode: 'rU' while trying to load binding.gyp

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# ValueError: invalid mode: 'rU' while trying to load binding.gyp

The node-gyp "ValueError: invalid mode: 'rU' while trying to load binding.gyp" was introduced in Python version 3.11.

There are 2 main ways to solve the error:

  1. Make a small change to your gyp/input.py file.
  2. Roll back your Python version to 3.10.

# Make a small change to your gyp/input.py file

Your input.py file should be located at:

  • on Windows - C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\gyp\pylib\gyp\input.py.

  • on macOS - /Users/YOUR_USER/npm-versions/YOUR_NPM_VERSION/node_modules/node-gyp/gyp/pylib/gyp/input.py.

  • on Ubuntu - /home/YOUR_USER/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py

You can also use the locate command to locate the directory on Linux.

shell
locate "pylib/gyp" | grep /pylib/gyp$

Once you find the input.py file, open it in your preferred text editor.

  1. Press Ctrl + F to look for the definition of the LoadOneBuildFile function.
  2. Press Ctrl + F to search for build_file_contents = open.

The following line:

input.py
# ⛔️ Incorrect - has rU build_file_contents = open(build_file_path, 'rU').read()

Should become the following line:

input.py
# ✅ Correct - has only r build_file_contents = open(build_file_path, 'r').read()

Notice that I removed the U after the r.

Once you make the change, save the file and the issue should be resolved.

# Rolling back Python to version 3.10

The error was introduced in Python 3.11.

You can check your Python version by issuing the following command.

shell
python --version # Or python3 python3 --version # Or py py --version

check your python version

You can downgrade your Python version to 3.10 by downloading it from the official page.

Once you scroll down to the Files section, you will see the download links.

scroll down to files section

If you use Anaconda, you can install a specific Python version using the following command:

shell
conda install python=3.10.6

conda install python 3 10

And you can create a virtual environment scoped to version 3.10 with the following command.

shell
conda create --name my_env -c anaconda python=3.10.6

Make sure to replace my_env with the name of your virtual environment.

If you are on macOS, you can also try to install python3.10 using brew.

shell
brew install python@3.10 export NODE_GYP_FORCE_PYTHON=/opt/homebrew/bin/python3.10

If the error persists, you can try to run the npm config set python command.

First, run the following command.

shell
npm install --python=python3.10

Now, issue the npm config set python command with the path to your python executable.

shell
npm config set python /path/to/python/executable

For example, on macOS, your path might look like: /opt/homebrew/bin/python3.10.

shell
npm config set python /opt/homebrew/bin/python3.10

On Linux, your path might look something like /usr/bin/python3.10.

shell
npm config set python /usr/bin/python3.10

On Windows, your path might look something like C:\Python3.10\python.exe.

shell
npm config set python C:\Python3.10\python.exe

# Updating your node-gyp version

You can also try to update your node-gyp version.

If you are on macOS or Linux, try issuing the following command.

shell
find ~/.nvm -type d -name "node-gyp" -exec sh -c 'cd "$(dirname "{}")" && npm i node-gyp@latest' \;

update your node gyp version

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.