Fix ‘ImportError: No Module Named _struct’ in Ubuntu

aptdpkgelementary-ospythonUbuntu

I'm running elementary OS 0.2 (based on Ubuntu 12.04) and starting today I'm seeing a weird error when attempting to install/upgrade packages. Just now for example I did a:

sudo apt-get update

which worked fine, followed by a:

sudo apt-get upgrade

which attempted returned this:

Setting up idle-python2.7 (2.7.3-0ubuntu3.5) ...
Traceback (most recent call last):
  File "/usr/lib/python2.7/compileall.py", line 16, in <module>
    import struct
  File "/usr/lib/python2.7/struct.py", line 1, in <module>
    from _struct import *
ImportError: No module named _struct
dpkg: error processing idle-python2.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 idle-python2.7
E: Sub-process /usr/bin/dpkg returned an error code (1)

What is going on? Obviously this is related to the python package but I can still run python scripts without issues. How can I fix this?


Add

Following the advice given in the answer I tried:

sudo apt-get --reinstall install python python-support

This returned:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 2 reinstalled, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
Need to get 0 B/192 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 864980 files and directories currently installed.)
Preparing to replace python 2.7.3-0ubuntu2.2 (using .../python_2.7.3-0ubuntu2.2_i386.deb) ...
Unpacking replacement python ...
Preparing to replace python-support 1.0.14ubuntu2 (using .../python-support_1.0.14ubuntu2_all.deb) ...
Unpacking replacement python-support ...
Processing triggers for man-db ...
Processing triggers for doc-base ...
Processing 1 changed doc-base file...
Registering documents with scrollkeeper...
Setting up idle-python2.7 (2.7.3-0ubuntu3.5) ...
Traceback (most recent call last):
  File "/usr/lib/python2.7/compileall.py", line 16, in <module>
    import struct
  File "/usr/lib/python2.7/struct.py", line 1, in <module>
    from _struct import *
ImportError: No module named _struct
dpkg: error processing idle-python2.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
Setting up python (2.7.3-0ubuntu2.2) ...
Setting up python-support (1.0.14ubuntu2) ...
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site.py", line 548, in <module>
    main()
  File "/usr/local/lib/python2.7/site.py", line 530, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/local/lib/python2.7/site.py", line 266, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/local/lib/python2.7/site.py", line 241, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/local/lib/python2.7/site.py", line 231, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
    return get_config_vars().get(name)
  File "/usr/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
    import re
  File "/usr/local/lib/python2.7/re.py", line 105, in <module>
    import sre_compile
  File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
    import sre_parse
  File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
    from sre_constants import *
  File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
    from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Errors were encountered while processing:
 idle-python2.7
E: Sub-process /usr/bin/dpkg returned an error code (1)

so it's a no go.

Best Answer

Searches for this turn up a couple of things to try.

  1. Re-install python

    Found this link: Broken python (2.7) after manually building and installing python 2.6. The suggestion was to re-install like so:

    $ sudo apt-get --reinstall install python python-support
    
  2. Make sure the software update app has internet enabled

    Found this link: more package dependency problems, i always get loads of problems after a fresh install.

    face palm I went into update manager/settings Ubuntu software tab and re-enabled all downloadable from internet buttons.

Related Question