4

How do I switch betwen Python 2 and 3 when using Flycheck with flake8 in Emacs?

Having installed Flycheck for Emacs, I then install a syntax-checker like flake8.

Installing flake8 using pip install flake8 makes Flycheck check Python 2 syntax.

However, using pip3 install flake8 makes Flycheck check the syntax for Python 3.

How can I switch between the modes for Python 2 and 3 in Emacs 24?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186

1 Answers1

2

If you use virtual environments, either using anaconda, miniconda or virtualenv, then I recommend that you use pyvenv.el. It requires a tiny bit of configuration which I may be able to help you out with if you need but it is generally pretty easy to setup. I use it to switch between different python versions and different virtual environments.

You should also be able to simply place a variable in your .dir-locals.el file. This file sets emacs variables based on the directory that you're in. If you want to set your python-interpetter based on the project or directory that you're in you can simply create a file called .dir-locals.el in the folder with the project files that you want to edit. In that file you should then have

((nil . ((python-shell-interpreter . "python3"))))

or

((nil . ((python-shell-interpreter . "python"))))

flycheck should then switch for you. Let me know if you have any questions. Also maybe take a look at this from the creator of flycheck

Jules
  • 973
  • 4
  • 10
  • Where can I find a tutorial for pyenv.el? It seems that nothing is showing up on Google. Also, what is a virtual environment used for? – Shuzheng Jun 16 '16 at 17:20
  • The link is broken. But I don't see any tutorials or documentation anywhere, do you? – Shuzheng Jun 16 '16 at 18:52