/
šŸ
Configuration for VSCode to code with Python
Search
Duplicate
Try Notion
šŸ
Configuration for VSCode to code with Python
To write code faster and cleaner we are going to configureĀ Visual Studio CodeĀ (VSCode) to auto format the code when saving each file.
Python createsĀ __pycache__Ā folders andĀ .pycĀ files in our code, to avoid having cache information displayed in the VSCodeā€™s file explorer we are going to configure it to ignore them.
Linting our code allow us to detect and fix errors, so we are going to install and configureĀ pre-commitĀ to automatically lint our code before every commit.
Make sure you are using a virtual environment
If doesnā€™t matter if you are creating aĀ FastAPIĀ api, aĀ FlaskĀ application or a simple script to automatize some boring task, you should always work on a virtual environment.
Having a virtual environment allows your systemā€™s python to be cleaner (without unnecessary libs installed) and itā€™s easier for you to test your project with different python versions.
Letā€™s create a folder for our project and generate a virtual environment inside of it:
mkdir vscode-configuration cd vscode-configuration python3 -m venv .venv
Shell
NOTE: Iā€™m going to make a tutorial to install python onĀ windowsĀ andĀ debian. If you are runningĀ ubuntuĀ and this command is giving you errors, you can installĀ python3-venv andĀ python3-dev:
sudo apt install python3-venv python3-dev
Shell
Configure virtual environment in your VSCode
You can open the vscode on your current folder typing: code .
OnĀ VSCodeĀ pressĀ F1Ā and type:Ā Python:select Interpreter, select your virtualenv or type the route, in our case is:Ā .venv/bin/python/
Select interpreter screen
Format the code after saving a file
To configure our current project we need to create a settings file, so letā€™s create theĀ .vscode/settings.jsonĀ file.
To format the code we need to instruct the editor what formatting provider we want to use, in my case iā€™m usingĀ yapf. We need to installĀ yapfĀ to be able to use it,Ā pip install yapf.
TheĀ settings.json file allow us to set args for our provider, this is an optional parameter but I prefer to use longer lines than the formatter defaults.
{ "python.formatting.provider": "yapf", "editor.formatOnSave": true, "python.formatting.yapfArgs": [ "--style={ based_on_style: pep8, column_limit: 120 }" ], }
JSON
.vscode/setting.json
IgnoreĀ pycacheĀ and *.pyc on file explorer
To avoid displaying this files on the VSCode we just need to add an option in ourĀ .vscode/setting.jsonĀ file:
{ ... , "files.exclude": { "**/*.pyc": {"when": "$(basename).py"}, "**/__pycache__": true }, }
JSON
.vscode/setting.json
This exclude the folder that are namedĀ __pycache__Ā and also every file ending inĀ .pyc if a file with the same name but with the extensionĀ .pyĀ exists in the same folder.
Pre-commit settings
In our root folder we are going to create a file to configure ourĀ pre-commitĀ tool (./.pre-commit-config.yaml) This is my configuration but you can set anyĀ hookĀ here:
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.4.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - id: requirements-txt-fixer - id: double-quote-string-fixer - repo: https://gitlab.com/pycqa/flake8 rev: 3.8.0 hooks: - id: flake8 additional_dependencies: [flake8-typing-imports==1.6.0] - repo: https://github.com/pre-commit/mirrors-yapf rev: v0.30.0 hooks: - id: yapf - repo: https://github.com/asottile/pyupgrade rev: v2.4.1 hooks: - id: pyupgrade args: [--py36-plus] - repo: https://github.com/asottile/reorder_python_imports rev: v2.3.0 hooks: - id: reorder-python-imports args: [--py3-plus] - repo: https://github.com/asottile/add-trailing-comma rev: v2.0.1 hooks: - id: add-trailing-comma args: [--py36-plus]
YAML
./.pre-commit-config.yaml
We are going to create aĀ ./setup.cfgĀ file with some configurations that are going to be used by theĀ hooks, this is usual my configuration file:
[autopep8] max_line_length = 120 [flake8] max_line_length = 120 [yapf] column_limit = 120 based_on_style = pep8
PowerShell
To useĀ pre-commit we first need to install it. So letā€™s runĀ pip install pre-commit.
Now that we haveĀ pre-commitĀ installed, we need to install theĀ hooksĀ on our project, we can do this runningĀ pre-commit install.
NOTE:Ā pre-commitĀ runs on every commit, so we need to make sure that our folder is a git repository. If you cloned your project from a service, likeĀ github, theĀ pre-commit install command will run with no problems. But if you are following this guide we need to runĀ git initĀ in our root folder before running theĀ pre-commit install command.
Pre-commit basic usage
Assuming you want to commit the changes in all your files you can run the following commands to lint the code.
git add.git commit -m "test commit"
Shell
This will run all the hooks on your modified files and attempt to fix what it can automatically. If the linter doesnā€™t need user intervention you can add the files that we automatically modified to your commit and recreate the commit.
git add . git commit -m "test commit"
Shell
If any hook is still giving you errors, itā€™s because they need manual intervention. For example if you are importing a file in the middle of your file, you need to manually move it to the top of the file. After making the changes, run theĀ git add .Ā andĀ git commit -m "test commit"Ā to finally have your commit ready to be pushed after successfully passing your linter.
Pre-commit run on all files
If you are installing pre-commit in a project that already have code written, you should lint everything so your following commits are cleaner.
pre-commit run --all-files
Shell
Pre-commit ignore line
If the linter wants to change a line but you donā€™t want to, you can specifically set that line to be ignored by the linter.
def test(): print( "THIS IS A VERY VERY VERY VERY VERY VERY VERY VERY VERY LONG LINE THAT I SHOULD BUT I DON'T WANT TO MAKE IT SHORTER", )
Python
The flake8 hook will fail with this message:
flake8.............................................Failed - hook id: flake8 - exit code: 1 main.py:3:121: E501 line too long (125 > 120 characters)
Shell
With that error we can tell the linter not to check the ruleĀ E501Ā for that line with the commentĀ # NOQA E501 at the end of the string:
def test(): print( "THIS IS A VERY VERY VERY VERY VERY VERY VERY VERY VERY LONG LINE THAT I SHOULD BUT I DON'T WANT TO MAKE IT SHORTER", # NOQA E501 )
Python
After this change the linter will accept our long line
flake8.............................................Passed
Shell