Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flake8 reports E999 SyntaxError atom Flake 8

Tags:

python

flake8

I am unable to solve the flake8 SyntaxError and although the code executes just fine.

enter image description here

Code without comments

import math


def answer(str_n):
    sume = ((str_n * (str_n + 1)) / 2) * math.sqrt(2)
    sume = int(sume)
    return sume


def answer1(str_n):
    sume = 0
    for i in range(str_n + 1):
        sume += math.floor(i * math.sqrt(2))
        # print i,math.floor(i*math.sqrt(2))
    return sume


print "Test answer:", answer(77)
print "Actual answer:", answer1(77)
like image 685
Omi Harjani Avatar asked Oct 14 '17 14:10

Omi Harjani


2 Answers

As @jonrsharpe says, and I agree, this is because the code is being run in Python 2, but linted in Python 3.

From the flake8 documentation on error codes:

We report E999 when we fail to compile a file into an Abstract Syntax Tree for the plugins that require it.

So to prove this is correct, using a file called bad_syntax.py and using the same print syntax as above:

print "test answer", len([])

When I run this with Python 2, everything is happy:

james@codebox:/tmp/lint$ python --version
Python 2.7.12
james@codebox:/tmp/lint$ python bad_syntax.py
test answer 0

Linting with flake8 invoked with a Python 2 environment also passes.

But when I lint with Python 3 (this is running in a virtualenv venv with Python 3 installed), the E999 is returned:

(venv) james@codebox:/tmp/lint$ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.5.2 on Linux
(venv) james@codebox:/tmp/lint$ flake8 bad_syntax.py
bad_syntax.py:1:19: E999 SyntaxError: invalid syntax

I do not think that this is a setting that needs changing inside linter-flake8 because Flake8 will use the version of Python that it is run through. My guess would be that Flake8 is being run on Python 3 because it has been installed inside a Python 3 environment, even though the code is being run on Python 2.

like image 119
jamesc Avatar answered Oct 02 '22 15:10

jamesc


Flake8 launcher has Python3 hardcoded as main python.

How to fix:

1) install flake8 package using pip

$ pip install flake8

pip will tell you that flake8 script hasn't been added to path and print path to it (/Library/Frameworks/Python.framework/Versions/2.7/bin/ in my case)

2) tune your IDE (Atom/PyCharm/etc) to use this script with your default Python 2.7 (my example is from PyCharm @ MacOS):

PyCharm -> Preferences -> External tools -> "flake8 - current file"
Program: /usr/local/bin/python 
Arguments: /Library/Frameworks/Python.framework/Versions/2.7/bin/flake8 --ignore=E501,E124,E127,E128 $FilePath$
Working directory: $FileDir$

[x] open console for tool output 
Output filters: $FILE_PATH$\:$LINE$\:.*

It will work correctly without reporting E999.has

PyCharm preferences screenshot

like image 27
Denis Barmenkov Avatar answered Oct 02 '22 15:10

Denis Barmenkov