AttributeError: the 'module' object does not have the 'maketrans' attribute

I have done some previous research about this error. There are some explanations in StackOverflow, the proposed solutions are completely unrelated.

When I try to import Gtk from gi.repository, it exits with the following output:
bash-4.2$ python3 Python 3.2 (r32:88445, Feb 21 2011, 21:11:06) [GCC 4.6.0 20110212 (Red Hat 4.6.0-0.7)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

 >>> from gi.repository import Gtk Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python3.2/site-packages/gi/importer.py", line 76, in load_module dynamic_module._load() File "/usr/lib64/python3.2/site-packages/gi/module.py", line 251, in _load overrides_modules = __import__('gi.overrides', fromlist=[self._namespace]) File "/usr/lib64/python3.2/site-packages/gi/overrides/Gtk.py", line 400, in <module> class MessageDialog(Gtk.MessageDialog, Dialog): File "/usr/lib64/python3.2/site-packages/gi/overrides/Gtk.py", line 404, in MessageDialog type=Gtk.MessageType.INFO, File "/usr/lib64/python3.2/site-packages/gi/module.py", line 127, in __getattr__ ascii_upper_trans = string.maketrans( AttributeError: 'module' object has no attribute 'maketrans' 

Since this is importing directly from the python console and not using the python script file, I don’t even know how to handle this.

+11
source share
4 answers

Ok, I managed to get it to work. Despite the dirty workaround:

I changed /usr/lib64/python3.2/site-packages/gi/module.py

on line 127 I replaced string.maketrans with str.maketrans so that it matches python 3 docs.

Hope to be helpful to everyone in my circumstances.

Hugo

+17
source

I tried to run string.maketrans using a Jupyter laptop, and this error message:

the module string has no maketrans attributes.

Changing the code to str.maketrans did the trick. However, it should be noted that I do not need to make any changes to:

 /usr/lib64/python3.2/site-packages/gi/module.py 
+5
source

This seems to be a known bug bug737375 , and it has been fixed (almost like Hugo's own solution).

You can find the fix in the main repository branch of pygopbject:
http://git.gnome.org/browse/pygobject/commit/?id=8f89ff24fcac627ce15ca93038711fded1a7c5ed

Anyway, I rewrote that in diff here, so maybe I can save you some time :)

From file: /usr/lib64/python3.2/site-packages/gi/module.py

You must replace:

 import string 

from:

 try: maketrans = ''.maketrans except AttributeError: # fallback for Python 2 from string import maketrans 

And replace again (around line 130):

 ascii_upper_trans = string.maketrans( 

from:

 ascii_upper_trans = maketrans( 
+4
source

Just replace string.maketrans with str.maketrans

As Iskren Stanislavov said in the answer below.

0
source