Flake8 reports E999 SyntaxError
Asked Answered
K

2

7

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

Atom screenshot

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)
Kainite answered 14/10, 2017 at 14:21 Comment(3)
Is this Python-2 or Python-3?Enki
You’re running this in Python 2.x but linting it as 3.xKerstinkerwin
I am running python 2.7, I looked in settings for linter-flake8 no mention for python version selection.Kainite
S
1

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.

Slattern answered 21/6, 2018 at 12:47 Comment(0)
F
1

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

Fauch answered 8/4, 2019 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.