Formatting Python Code to pass the Maintainability test can be hard; especially if you are not receiving some help. It's in human nature to get tired of redundancy, we easily want to get the job done quickly and move on. However, this affects our work output and the quality of our work. When it comes to code quality it's paramount to maintain standards, there's no better way to do so than to follow some already set standards.
PEP8 defines Python coding standards; from variable declaration to formatting of classes. It has it all, this allows you to nicely format your python code. To install the package ensure you are in your project folder and virtualenv
is enabled, if not run the following lines in your folder directory
$ virtualenv env
$ source env/bin/activate
Then install PEP8
$ pip install pep8
Now let's checkout Pylint, this tool checks whether we follow PEP8 standards and returns errors where we fail to follow. Furthermore, this tool also does error checking due to syntax errors. To install pylint run the following code;
$ pip install pylint
Since we now have the two needed tools we can now open vs code
$ code .
Once we open our vs code editor; we can select our preferred interpreter, just press
Ctrl + Shift + P
.
You should see something similar to the above result. Next we select our Python Interpreter
Just select the python3/2 with virtualenv enabled. This will ensure that Vs code picks up tools we installed in virtual env.
Next we finally activate linting on Vs code.
Follow the following steps
File > Preferences > Settings > Workspace Settings > Python Configuration
Click Edit in settings.json
Your workspace should match the above linting settings. After editing your json save the settings and start coding. Now everytime you access Vs Code in virtaulenv it will always activate linting.
Top comments (16)
nowadays (june 2020) I get a message of
Unknown configuration setting
for pep8has it been renamed flake8 as that does get recognized?
I am trying with various options like this to toggle on/off various linter
flake8
is a different linter entirely. If you're still looking for PEP 8 formatting, you can installautopep8
and change your"python.formatting.provider"
setting to"autopep8"
.pep8 has been renamed to pycodestyle, so the
python.linting.pep8Enabled
is not a valid setting anymore. Now it ispython.linting.pycodestyleEnabled
well where is the option to open these JSON settings as in ur screenshot
In Windows you activate a virtual environment by running the following command:
or:
That was simple and straight to the point. Could you write an article to setup vscode for python for an absolute starter. Because, I have setup the venv and I am able to run the python file in pycharm but not in vscode. Any light on that would be great. :)
Sure, I'll have it up pretty soon
Maybe you can give
wemake-python-styleguide
a try? It has even more rules than pylint, but does not even try to mess with types.It has way less false-positives and is based on
flake8
.wemake-services / wemake-python-styleguide
The strictest and most opinionated python linter ever!
wemake-python-styleguide
Welcome to the strictest and most opinionated python linter ever.
wemake-python-styleguide
is actually a flake8 plugin with some other plugins as dependencies.Quickstart
You will also need to create a
setup.cfg
file with the configuration.We highly recommend to also use:
Running
This app is still just good old
flake8
And it won't change your existing workflow.See "Usage" section in the docs for examples and integrations.
We also support Github Actions as first class-citizens Try it out!
What we are about
The ultimate goal of this project is to make all people write exactly the same
python
code.Cheers!
Thank you!
This is no longer working in VSCode 1.37.1 (2019-08-15). If I follow your instructions then run linting I get a message "Linter pep8 is not installed". Installing again doesn't fix it. Clicking "Select Linter" gives you a list of all supported linters but if you select pep8 it then says "Multiple linters are enabled in settings. Replace with 'pep8'?
I think the instructions in here might gelp help. In a nutshell:
pip install pylint
Let me know if it still doesn't work for you.
This article has been wholesale plagiarised at Medium by Hemprasad Badgujar (incl. all the different misspellings of Visual Studio Code):
medium.com/seminal/setting-up-pep8...
thanks for pointing this out
Clear and concise article John.
Useful guide, thank you!
I appreciate your feedback