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