Shamshun
Shamshun

Reputation: 482

My python2.7 suddenly stopped working, complaining about a python3.5 script

My python27 and python35/36 installations were working alright up to now. Now suddenly, when I run python2 on my terminal, all I get is :

$ python2

  File "/usr/lib/python3.5/site.py", line 182
    file=sys.stderr)
        ^
SyntaxError: invalid syntax

Why is python2 trying to access python3 site-packages script? How can I fix this?
(Ubuntu 16.04 OS)

edit: That's all the output. If I use the verbose switch:

$ python2 -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
  File "/usr/lib/python3.5/site.py", line 182
    file=sys.stderr)
        ^
SyntaxError: invalid syntax
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] zipimport
# cleanup[1] signal
# cleanup[1] exceptions
# cleanup[1] _warnings
# cleanup sys
# cleanup __builtin__
# cleanup ints: 5 unfreed ints
# cleanup floats

Upvotes: 0

Views: 243

Answers (2)

ShadowRanger
ShadowRanger

Reputation: 155683

Converting comment to answer on request:

You say your .bashrc contains the line:

export PYTHONPATH="${PYTHONPATH}:/usr/lib/python3.5" 

PYTHONPATH is a hack, and shouldn't be set by default unless you're personally hacking in specific directories of your own personally developed modules you were too lazy to convert to installable packages (which must be fully Python 2/3 compatible if you want both to work). By having /usr/lib/python3.5 in your PYTHONPATH it says that all versions of Python (not just 3.5) should look there before they look in the default module lookup locations when imports are performed.

To fix, all you need to do is:

  1. Comment out that line from your .bashrc (to prevent the problem in the future)
  2. Run unset PYTHONPATH in existing terminals to fix the problem immediately (or just log out and back in after step 1)

Upvotes: 2

Hicham Zouarhi
Hicham Zouarhi

Reputation: 1060

when working with multiple python versions always make sure that PYTHOPATH and PYTHONHOME are pointing on the right directories,the first should point to your python bin folder and the second to the lib folder where the right site-packages are

Upvotes: 0

Related Questions