0

I am running emacs24 and I'm new to emacs. I have some code in Python 2.7 that I am checking with flycheck. When I check the syntax, I get:

error F821 undefined name 'xrange' (python-flake8)

I understand that xrange is not in Python3, but here I'm on Python 2.7. I guess it's configured to run on Python 3, since also raw_input yields the same error.

How do I fix this?

Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185

2 Answers2

0

Flycheck does not care about the difference between Python 2 and Python 3. It runs the first flake8 executable it finds in exec-path, and in your case, that's apparently a flake8 installed for Python 3.

You need to install flake8 for Python 2 and point Flycheck to that executable, either by putting the target directory in exec-path or with M-x flycheck-set-checker-executable.

I recommend to use a dedicated Python 2 virtualenv for your project and make Emacs and set python-shell-virtualenv-root to that directory in Python buffers (for instance with Directory Variables). Then you can point exec-path to that virtualenv. With a little custom Emacs Lisp you can even automate that.

0

I were struggling with same problem, and can recommend use my solution for that: https://github.com/rmuslimov/flycheck-local-flake8. It's trivial - and it will force flycheck use proper flake8 executable from your virtualenv.

I'd recommend add setup.cfg to each python project you work on. Flake8 has some environment variables which may be defined there. For example here is mine:

[metadata]
name=fastttrace
version=release.5.9.0

[flake8]
exclude = tests/*, migrations/*
ignore = D100,D101,D102,D103,D205,D400,E731
import-order-style = google
max-complexity = 15

It allows you have separate flake8 rules per project and store them in repo, which is convenient way to share with other devs.

Rustem
  • 2,884
  • 1
  • 17
  • 32