Tuesday, January 21, 2025

Resolving the ValueError: invalid mode: 'Ur' Error when Installing the Wordnet Package

Introduction

When attempting to install the Wordnet package using Python 3.11.8 on an x86_64 architecture, you may encounter a frustrating error. This blog post will guide you through resolving the ValueError: invalid mode: 'Ur' error and successfully installing the Wordnet package.

The Error

The error occurs during the installation process, specifically when running python setup.py egg_info. The error message is as follows:

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [10 lines of output]
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-roglyr1e/wordnet_b5a4abfe71b04a169e19d2ba1bab9a46/setup.py", line 24, in <module>
long_description=read('README.md'),
^^^^^^^^^^^^^^^^^
File "/tmp/pip-install-roglyr1e/wordnet_b5a4abfe71b04a169e19d2ba1bab9a46/setup.py", line 7, in read
return open(os.path.join(os.path.dirname(file), fname),'Ur').read()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid mode: 'Ur' 

The Solution

The error occurs due to the use of an invalid file open mode 'Ur'. To resolve this issue, you need to modify the setup.py file to use a valid mode.

Here's the corrected code:
--- setup.py 2025-01-21 04:55:36.541546072 -0800
+++ setup.py.old 2025-01-21 04:55:25.068593241 -0800
@@ -4,7 +4,7 @@
string in below ...
import os
def read(fname):
return open(os.path.join(os.path.dirname(file), fname),'r').read()
return open(os.path.join(os.path.dirname(file), fname),'Ur').read()

Replace the 'Ur' mode with 'r' in the read function. The corrected line should look like this:
return open(os.path.join(os.path.dirname(file), fname),'r').read()

Clone the wordnet to get the source and then do above change. Here are commands I used.

git clone https://github.com/anuragkumarak95/wordnet.git
cd wordnet
pip3 install -r requirements.txt
python3 setup.py install --verbose

Conclusion

By modifying the setup.py file to use a valid file open mode, you should be able to successfully install the Wordnet package. 

No comments: