Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'module' object has no attribute 'maketrans'

Tags:

python-3.x

I've been doing some previous research about this error. There are some explanations here in StackOverflow related, the solutions suggested are quite unrelated though.

When I try to import Gtk from gi.repository, it crashes 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 an import straight from python console and not by executing a python file script I don't even have a clue how to handle this.

like image 347
Hugo Avatar asked Jul 08 '11 17:07

Hugo


3 Answers

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

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

in line 127 I replaced string.maketrans with str.maketrans so it complies with python 3 docs.

Hope to be helpful for anyone in my circumstances.

Hugo

like image 139
Hugo Avatar answered Nov 04 '22 11:11

Hugo


I was trying to run string.maketrans using Jupyter notebook and it the error message:

the module string has no attribute maketrans.

Changing the code to str.maketrans did the trick. It must be noted however that I did not have to make any changes to the:

/usr/lib64/python3.2/site-packages/gi/module.py
like image 42
Stats_Lover Avatar answered Nov 04 '22 11:11

Stats_Lover


This seems to be a known bug bug737375 and it was fixed (almost like Hugo own solution).

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

Anyway I'll rewrite here what's in the diff, so maybe I'll save you some time :)

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

You should replace:

import string

with:

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

And again replace (around line 130):

ascii_upper_trans = string.maketrans(

with:

ascii_upper_trans = maketrans(
like image 8
Rik Poggi Avatar answered Nov 04 '22 13:11

Rik Poggi