2

Using Ubuntu 16.04. In the main environment the following problem is not there. But I am using Docker and there I am facing the following issues.

Have both python version 2.7(2.7.12) and 3.5(3.5.2). While using python3.5 it sometimes give error such as : .... .... File "/usr/lib/python2.7/urllib2.py", line 254 raise AttributeError, attr ^ SyntaxError: invalid syntax

And python2.7 can not be used.

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

Seems, it is mixing-up with 3.5 and 2.7 and creating problems for both. Had searched for long, but could not found a solution yet. As it is suggested not to uninstall 2.7, so tried to 'remove' 3.5, but it ends up with error. I had installed 3.6 also. But it also can't used.

Some other info, in case needed.

$ python --version
Python 2.7.12
$ python2 --version
Python 2.7.12
$ python3 --version
Python 3.5.2

$python2.7 -m site --user-site
File "/usr/lib/python3.5/site.py", line 182
    file=sys.stderr)
        ^
SyntaxError: invalid syntax

$python3.5 -m site --user-site
/root/.local/lib/python3.5/site-packages

Under /usr/local/lib/python3.5/site-packages, there are a whole lots of packages, preinstalled and those I had installed later on.

Whereas, under /usr/local/lib/python2.7/ there are two folders, /dist-packages and /site-packages and both are empty.

I have mainly installed the packages using pip and pip3.5.

$pip --version
pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

$pip3 --version
pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

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

Adding as a response to those had kindly suggested solutions.

(1) Tried to remove 3.5 and 3.6, but both end up with error.

Tried to remove 3.5 and 3.6, but ends up giving error. Even when trying to say, 3.6 with various other it gives the same error msg as,

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

Besides, after reading some other posting, it seems removing a python version may cause other problems. I am new to Python and not sure about its environment setting ets.


(2) Have tried this sample code for testing.

from sklearn import datasets
iris = datasets.load_iris()    
X = iris.data
y = iris.target    
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.5)
from sklearn.neighbors import KNeighborsClassifier
my_classifier = KNeighborsClassifier()
my_classifier.fit(X_train, y_train)
predictions = my_classifier.predict(X_test)    
from sklearn.metrics import accuracy_score
print (accuracy_score(y_test, predictions))

Gave the following error.

python3.5 iris_classifier.py 
Traceback (most recent call last):
  File "iris_classifier.py", line 1, in <module>
    from sklearn import datasets
  File "/usr/local/lib/python3.5/dist-packages/sklearn/datasets/__init__.py", line 25, in <module>
    from .mldata import fetch_mldata, mldata_filename
  File "/usr/local/lib/python3.5/dist-packages/sklearn/datasets/mldata.py", line 12, in <module>
    from urllib2 import HTTPError
  File "/usr/lib/python2.7/urllib2.py", line 254
    raise AttributeError, attr
                        ^
SyntaxError: invalid syntax
Debacs
  • 29
  • 1
  • 3

2 Answers2

0

I had the same issue. Uninstalling 3.5 completely, making sure to remove it from the PATH in system environment variables, worked for me.

Was then able to reinstall later with no issues.

  • Tried to remove 3.5 and 3.6, but ends up giving error. Even when trying to say, 3.6 with various other it gives the same error msg as, File "/usr/lib/python3.5/site.py", line 182 file=sys.stderr) ^ SyntaxError: invalid syntax – Debacs Jul 17 '18 at 09:28
0

You can either:

  • Create virtual environments for your Python 2.7 projects and Python 3 projects.
  • Set a Python alias: alias python=/usr/local/bin/python2.7
  • If you are trying to run your script (.py) from your terminal, you can also specify which python version you want to compile. Linux Python
    • python ./hello.py: Will run it as Python 2.7
    • python3 hello.py: Will run it as Python 3.5

Although the question might be different, but this might help. Two versions of python on linux. how to make 2.7 the default

Mert Karakas
  • 178
  • 1
  • 4
  • 22
  • Sorry if I misunderstood you. Setting 2.7 or 3.5 as my default is python is not my issue. Whatever works well is fine with me. I am trying some machine learning related packages, so either of them is fine if they don't throw exception time and again. – Debacs Jul 17 '18 at 09:55