3

I accidentally replace python binary files located at /usr/local/bin/. Since then, I can't run any python script or even run the interactive mode of python.

$ python
Traceback (most recent call last):
  File "/usr/lib/python2.7/site.py", line 562, in <module>
    main()
  File "/usr/lib/python2.7/site.py", line 544, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/lib/python2.7/site.py", line 271, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/lib/python2.7/site.py", line 246, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/lib/python2.7/site.py", line 236, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/lib/python2.7/sysconfig.py", line 558, in get_config_var
    return get_config_vars().get(name)
  File "/usr/lib/python2.7/sysconfig.py", line 457, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/lib/python2.7/sysconfig.py", line 303, in _init_posix
    makefile = _get_makefile_filename()
  File "/usr/lib/python2.7/sysconfig.py", line 297, in _get_makefile_filename
    return os.path.join(get_path('platstdlib').replace("/usr/local","/usr",1), "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'

I have see related questions with the "AttributeError: 'module' object has no attribute 'pydebug' " error, but I still can't figure out how to solve this problem.

When I try to re-install appears the same error:

$ sudo apt-get install python2.7-minimal
[sudo] password for : 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python2.7-minimal is already the newest version.
The following packages were automatically installed and are no longer required:
  libgsasl7 libmailutils2 libntlm0
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 402 not upgraded.
4 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? Y
Setting up python2.7-minimal (2.7.1-5ubuntu2.2) ...
Traceback (most recent call last):
  File "/usr/lib/python2.7/site.py", line 562, in <module>
    main()
  File "/usr/lib/python2.7/site.py", line 544, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/lib/python2.7/site.py", line 271, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/lib/python2.7/site.py", line 246, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/lib/python2.7/site.py", line 236, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/lib/python2.7/sysconfig.py", line 558, in get_config_var
    return get_config_vars().get(name)
  File "/usr/lib/python2.7/sysconfig.py", line 457, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/lib/python2.7/sysconfig.py", line 303, in _init_posix
    makefile = _get_makefile_filename()
  File "/usr/lib/python2.7/sysconfig.py", line 297, in _get_makefile_filename
    return os.path.join(get_path('platstdlib').replace("/usr/local","/usr",1), "config" + (sys.pydebug and "_d" or ""), "Makefile")
AttributeError: 'module' object has no attribute 'pydebug'
dpkg: error processing python2.7-minimal (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of python2.7:
 python2.7 depends on python2.7-minimal (= 2.7.1-5ubuntu2.2); however:
  Package python2.7-minimal is not configured yet.
dpkg: error processing python2.7 (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of libpython2.7:
 libpython2.7 depends on python2.7 (= 2.7.1-5ubuntu2.2); however:
  Package python2.7 is not configured yet.
dpkg: error processing libpython2.7 (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python2.7-dev:
 python2.7-dev depends on python2.7 (= 2.7.1-5ubuntu2.2); however:
  Package python2.7 is not configured yet.
 python2.7-dev depeNo apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                                             No apport report written because the error message indicates its a followup error from a previous failure.
                                                                          No apport report written because MaxReports is reached already
                                                                                                                                        nds on libpython2.7 (= 2.7.1-5ubuntu2.2); however:
  Package libpython2.7 is not configured yet.
dpkg: error processing python2.7-dev (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 python2.7-minimal
 python2.7
 libpython2.7
 python2.7-dev
E: Sub-process /usr/bin/dpkg returned an error code (1)
Geparada
  • 2,898
  • 9
  • 31
  • 43
  • Why not reinstall Python? – Isaac Apr 29 '13 at 16:24
  • 1
    I already tried and doesn't work – Geparada Apr 29 '13 at 16:25
  • What is your operating system? – Mirko Cianfarani Apr 29 '13 at 16:26
  • 2
    Problem might be it's hitting `/usr/local/bin/python` instead of `/usr/bin/python`. If `which python` returns `/usr/local/bin/python`, then move it out of the way, or just run `/usr/bin/python` explicitly. – Aya Apr 29 '13 at 16:35
  • All the said paths seem to refer to `/usr/lib/`... – glglgl Apr 29 '13 at 16:36
  • @glglgl Indeed. Which suggests that whatever is at `/usr/local/bin/python` is using a different version of the `sys` module than you'd expect with Python 2.7. – Aya Apr 29 '13 at 16:38
  • @Aya you're right! which python returns /usr/local/bin/python, and when I run /usr/bin/python explicitly python works! But how can I set the /usr/bin/python as the default python? – Geparada Apr 29 '13 at 16:40
  • If your in a virtualenv that is located in a path containing `/usr/local` you're hitting a bug in sysconfig.py, see https://github.com/pypa/virtualenv/issues/118 . One fix is to move the virtualenv to another location. – Wernight Jul 22 '14 at 10:07

3 Answers3

4

The problem is that you're hitting /usr/local/bin/python instead of /usr/bin/python.

You can either move it out of the way by running something like...

mv /usr/local/bin/python /usr/local/bin/python.old

...as root, or if you're sure you don't need it, then just delete it with...

rm /usr/local/bin/python

If bash still tries to run /usr/local/bin/python, and running type python prints...

python is hashed (/usr/local/bin/python)

...then do hash -r to clear the hash table.

It's not a good idea to copy /usr/bin/python to /usr/local/bin/python, otherwise you may get problems the next time you update python with apt-get upgrade, i.e. it'll update /usr/bin/python, but /usr/local/bin/python will still be run by default.

Aya
  • 39,884
  • 6
  • 55
  • 55
  • Yeah, that's the problem... but when I mv /usr/local/bin/python /usr/local/bin/python.old bash: /usr/local/bin/python: No such file or directory ... how can I tell bash to use /usr/bin/python insted of /usr/local/bin/python??? – Geparada Apr 29 '13 at 16:47
  • cp /usr/bin/python /usr/local/bin/ was the simple solution. Thanks!!! – Geparada Apr 29 '13 at 16:49
  • 1
    @Geparada If you're certain there's no file at `/usr/local/bin/python`, then do `hash -r` to clear the bash hash table, and it should pick up `/usr/bin/python`. – Aya Apr 29 '13 at 16:49
  • That's exactly the problem that I have now. I moved usr/local/bin/python to usr/local/bin/python.old ...and then run hash -r. Now python is hashed in /usr/bin/python, but I still can't run apt-get install. – Geparada May 28 '13 at 20:54
  • @Geparada If you're having a new issue with `apt-get` it's probably best to open a new question with the details. – Aya May 29 '13 at 12:54
2

Looks like this has something to do with a Python build made specially for debugging.

I'd suggest you reinstall Python with apt-get:

sudo apt-get install --reinstall python2.7

Your question seems to be the same like this SO question about sys.pydebug

Community
  • 1
  • 1
halflings
  • 1,540
  • 1
  • 13
  • 34
0

You have to reinstall. This is a better solution. This is the my favoruite installation with terminal on Ubuntu. Try: Install Python and cherrypy

And you look on the answer on this question

Installation python

Community
  • 1
  • 1
Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39