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

<pre><code>error F821 undefined name 'xrange' (python-flake8)
</code></pre>

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

<p>How do I fix this?</p>

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

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

<p>I recommend to use a dedicated Python 2 virtualenv for your project and make Emacs and set <code>python-shell-virtualenv-root</code> to that directory in Python buffers (for instance with <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html" rel="nofollow">Directory Variables</a>).  Then you can point <code>exec-path</code> to that virtualenv.  With a little <a href="https://github.com/lunaryorn/.emacs.d/blob/master/lisp/flycheck-virtualenv.el" rel="nofollow">custom Emacs Lisp</a> you can even automate that.</p>

python emacs flake8 flycheck