Reputation:
Here is my output of flake8
during validation:
Traceback (most recent call last):
File "/usr/local/bin/flake8", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/flake8/main.py", line 25, in main
flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
File "/usr/local/lib/python2.7/dist-packages/flake8/engine.py", line 244, in get_style_guide
options.exclude.extend(pep8.normalize_paths(EXTRA_EXCLUDE))
AttributeError: 'module' object has no attribute 'normalize_paths'
Why can't I use it?
Upvotes: 12
Views: 6666
Reputation: 6298
This problem can be fixed by using an up-to-date version of flake8
, specifically flake8 >= 2.6.0
which does not rely on the pep8
module anymore (Source). You can do that by installing it via pip:
$ pip install flake8
which should install the newest version (3.5.0 at the moment).
This error tells you that flake8 does find a module named pep8
, but that module does not contain a function named normalize_paths
. There can be multiple reasons for that, including a broken installation (which should be fixed be reinstalling flake and pep8) and conflicting versions of pep8
in your path. The latter can happen when you modify your sys.path
and/ or somehow manage to get an own module named pep8 into your path. This can be as little as a folder named pep8 with an __init__.py
as in this example:
$ mkdir /home/me/miniconda3/envs/fl8/lib/python2.7/site-packages/pep8
$ touch /home/me/miniconda3/envs/fl8/lib/python2.7/site-packages/pep8/__init__.py
$ flake8 foo.py
Traceback (most recent call last):
File "/home/me/miniconda3/envs/fl8/bin/flake8", line 7, in <module>
from flake8.main import main
File "/home/me/miniconda3/envs/fl8/lib/python2.7/site-packages/flake8/main.py", line 8, in <module>
from flake8.engine import get_parser, get_style_guide
File "/home/me/miniconda3/envs/fl8/lib/python2.7/site-packages/flake8/engine.py", line 11, in <module>
from flake8.reporter import (multiprocessing, BaseQReport, FileQReport,
File "/home/me/miniconda3/envs/fl8/lib/python2.7/site-packages/flake8/reporter.py", line 18, in <module>
class BaseQReport(pep8.BaseReport):
AttributeError: 'module' object has no attribute 'BaseReport'
As you see this error is a little different that yours but of the same kind. This can be due to minor changes in the pep8
module.
To see if everything went well you can check the content of your python path and the location of the pep8
module.
$ python
>>> import pep8
>>> pep8.__file__
/home/me/miniconda3/envs/fl8/lib/python3.5/site-packages/pep8.py
This tells you which pep8
module your Python is using. So this should point somewhere into your Python distribution and not into one of your local folders. If it does point into your files this is an issue with your sys.path
, the list of paths that python checks during imports:
>>> import sys
>>> import pprint # this is only used to make it print pretty
>>> pprint.pprint(sys.path)
['',
'/home/me/miniconda3/envs/dptest/lib/python35.zip',
'/home/me/miniconda3/envs/dptest/lib/python3.5',
'/home/me/miniconda3/envs/dptest/lib/python3.5/plat-linux',
'/home/me/miniconda3/envs/dptest/lib/python3.5/lib-dynload',
'/home/me/miniconda3/envs/dptest/lib/python3.5/site-packages',
'/home/me/miniconda3/envs/dptest/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg']
This shows you all folders that Python searches the pep8
module in (from top to bottom). So if there is a path in there that does point to a location where you would not expect a python package to live, that might be the culprit.
(Further Reading: reddit thred)
If using the newest version does not sort out your problem you can try using conda to create a virtual environment in which you can install flake8
and all of its dependencies to avoid interactions with other parts of your python installation. After downloading conda, create a new environment (mine is named fl8), activate it, install flake8, and use it:
$ conda create -n fl8 python=2.7 # if you insist on python 2.7
$ source activate fl8
(fl8) $ pip install flake8
[...]
$ flake8 foo.py
If you launch Emacs from the command line, it uses the same environment as the shell you launched it from. So in order to use flake8 from an environment, like above, you have to activate the environment first. I set up my Emacs with elpy following this tutorial. To make it work with conda I also installed flake etc. into the root environment.
If you launch Emacs a different way (for example using M-F2 on Ubuntu or from the start menu) the environment might not work correctly. I did not fully understand how to fix that yet.
Upvotes: 2