'ImportError: No module named _struct' error when managing packages

22,979

Solution 1

I found a way to solve this issue by simply removing the idle-python2.7 package:

sudo apt-get remove idle-python2.7  

as stated here.

Solution 2

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.

Solution 3

The answer is simple. Your system is misconfigured. You did a local install of base Python packages. This is a no-no. You should install the base binary Python packages from your distributions repository. In particular, your Python installation is looking for files like lib/python2.7/site.py and lib/python2.7/sysconfig.py in /usr/local, but these files are part of the base Python package python2.7-minimal, or possibly libpython2.7-minimal, depending on what version of Debian/Ubuntu your distribution corresponds to.

Anyway, the solution is simple. Remove the locally installed base Python packages, and install the basic Python packages from your distribution. If the software is not available in your distribution, then install it locally. There is really not much point using a distribution if you don't use their packages.

What does

apt-cache policy python2.7-minimal libpython2.7-minimal python2.7

say? Two of these should be installed on your system.

Share:
22,979

Related videos on Youtube

Gabriel
Author by

Gabriel

Updated on September 18, 2022

Comments

  • Gabriel
    Gabriel over 1 year

    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.

    • derobert
      derobert almost 10 years
      FYI, LC_ALL=C sudo apt-get upgrade should get you messages in English. Also, have you done anything weird/special to this machine?
    • karel
      karel over 9 years
      How do you install Python packages? Do you use the Software Center or do you also use other ways of installing Python packages such as pip Alternative Python package installer or installing Python packages from .tar.gz archives?
    • Gabriel
      Gabriel over 9 years
      I use sudo pip install since just pip install doesn't work in my case (no idea why).
    • karel
      karel over 9 years
      Pip doesn't work as reliably as installing packages from the Software Center. This answer by Oli describes a few ways of managing this difficult situation, which are themselves difficult, but they help to protect your operating system. While this doesn't answer your question, it's still makes sense to be careful about the way you use pip: How to install pip (python) to user without root access.
  • Gabriel
    Gabriel almost 10 years
    slm: unfortunately your suggestion did not work (see updated question for details) As for 2., it is enabled.