Lädt...


🔧 Linting Python in VS Code [Video]


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

One of the most important parts of writing code is making sure your code is readable. There are so many positive downstream effects of clean code from its ease to maintain and add features, debug subtle programming errors or find uninitialized variables. Some people call this code hygiene. VS Code has linter extension support that enables you develop faster, produce cleaner code and is tweaked to your set up.

How VS Code handles Python linters

The linter development experience can live entirely within VS Code if you'd like. This means you do not need to pip install pylint for example.

🔥PRO-TIP: Set your default importStrategy
importStrategy is a field on any of the Python linter extensions for automatically setting the linter version in your IDE. I like to set mine to fromEnvironment in my User level settings so that it automatically checks the version in any project I'm working on with a team, but also allow VS Code to default to any Workspace level settings my team is sharing.

settings.json

// use the pylint version with extension
"pylint.importStrategy": "useBundled"

// use the pylint version found in requirements
"pylint.importStrategy": "fromEnvironment"

When you start your project, the first thing you will likely do is activate your virtual environment or open up a container to develop in (like Dev Containers). Linters are the development tool that is used to make sure your code is formatted consistently across your team. Then add your linter to your requirements-dev.txt-- or otherwise named file to store your development only requirements -- and pip install -r requirements-dev.txt. In VS Code, you'll select your Python interpreter by using shortcut key Ctrl+P to open up the Command Pallette, and select from the dropdown menu which environment you'd like to use for your project. I select the interpreter associated with my project environment.

Screen shot of the Command Pallette dropdown menu. The top of the menu allows the user to search for a name. The first selection is the option to add an interpreter path. The following options are different Python versions VS Code has found on my machine. The last of five options is starred and "recommended" as the Python interpreter connected to my Python virtual environement.

There are many packages for code quality. At the time of this post, VS Code and its active extension-contributing community supports flake8, ruff, pylint and the newly released mypy. The Ruff linter was created from the Python Extension template by Charlie R. Marsh. The VS Code team encourages community contributed packages and you can learn more in the VS Code documentation.

🔥PRO-TIP: VS Code extension works as soon as its installed
VS Code linting is automatically enabled once the extension is installed. You no longer need python.linting.enabled set to true under settings.json.

Enable what you want and disable what you don't

Navigate to the activity bar to the far left and search for "Pylint." I often like to enable pre-release so I can get the latest features and report bugs if I come across them, doing my part for the community.

GIF: Enable Pylint
Enabling Pylint from the extensions panel

🔥PRO-TIP: Install isort
Install an import sorting extension like isort then use the shortcut key Shift+Alt+Oin order to sort your imports quickly.

🔥PRO-TIP: Toggle Problems tab
Use Ctrl+Shift+M to toggle open and close the Problems tab in VS Code.

GIF: Solving my first problems in VS Code for a Wagtail project
Open problems tab in VS Code and sort import order of code

You can specify problems to consistently ignore in your projects by adding disable flags to your settings. You can do this either in your settings panel Ctrl+, or with your settings.json Ctrl+P then typing "Settings JSON" in the text bar.

Here's what like to disable a few doc string problems among other arguments in Pylint. "Args" are always lists in brackets:
settings.json

"pylint.arg": [
    "--reports",
    "12",
    "--disable=C0114",
    "--disable=C0115",
    "--disable=C0116",
]

You can also add these same arguments via your User or Workspace settings panel. The same settings work for other linter extensions:
settings.json

"flake8.arg": ["--ignore=E24,W504", "--verbose"],
"ruff.args": ["--config=/path/to/pyproject.toml"]

Image description

🔥PRO-TIP: Enable lintOnSave
When enabling lintOnSave, you might also want to enable generic files.autoSave option. The combination provides frequent linting feedback in your code as you type.

Want to dig more into the VS Code Linting documentation? https://code.visualstudio.com/docs/python/linting

Keep in touch!

I host The Python Pulse every second Friday of the month https://aka.ms/python-pulse-live

Python Pulse thumbnail - Getting the most out of Python with VS Code and Azure

... or join me on the Microsoft Python Discord

More Reading...

...

🔧 Linting Python in VS Code [Video]


📈 40.69 Punkte
🔧 Programmierung

🔧 Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality


📈 36.21 Punkte
🔧 Programmierung

🔧 Formatting and Linting Your Python Codes with GitHub Actions


📈 32.44 Punkte
🔧 Programmierung

🎥 Formatting and Linting | More Python for Beginners [2 of 20]


📈 32.44 Punkte
🎥 Video | Youtube

🔧 Set up linting and formatting for code and (S)CSS files in a Next.js project


📈 30.17 Punkte
🔧 Programmierung

🔧 Alternatives to Prettier – Popular Code Linting and Formatting Tools


📈 30.17 Punkte
🔧 Programmierung

🔧 A Guide to ESLint, Prettier, and VSCode Setup for Code Linting & Formatting


📈 30.17 Punkte
🔧 Programmierung

🔧 Adding code formatting, linting, pre-commit hooks and beyond...


📈 30.17 Punkte
🔧 Programmierung

🔧 Linting


📈 26.39 Punkte
🔧 Programmierung

🔧 Conventional Commit Message and Linting


📈 26.39 Punkte
🔧 Programmierung

🔧 Linting with Eslint


📈 26.39 Punkte
🔧 Programmierung

🔧 Static Testlash: Linting va Formatting


📈 26.39 Punkte
🔧 Programmierung

🐧 Introducing KubeLinter, an open source linting tool for Kubernetes, from StackRox


📈 26.39 Punkte
🐧 Linux Tipps

📰 Pufferüberlauf in python-crcmod, python-cryptography und python-cryptography-vectors (SUSE)


📈 18.13 Punkte
📰 IT Security Nachrichten

🔧 Introducing More Python for Beginners | More Python for Beginners [1 of 20] | More Python for Beginners


📈 18.13 Punkte
🔧 Programmierung

🔧 What Makes Python Python? (aka Everything About Python’s Grammar)


📈 18.13 Punkte
🔧 Programmierung

🔧 Python for Beginners [1 of 44] Programming with Python | Python for Beginners


📈 18.13 Punkte
🔧 Programmierung

📰 Any books similar to Black Hat Python, or Violent Python that use Python v3?


📈 18.13 Punkte
📰 IT Security Nachrichten

🐧 Switching Geany to execute Python files as Python 3, not Python 2


📈 18.13 Punkte
🐧 Linux Tipps

🎥 🔴 Python Pulse | Exploring the VS Code test flow for Python


📈 15.87 Punkte
🎥 Video | Youtube

🔧 Python Debugging Handbook – How to Debug Your Python Code


📈 15.87 Punkte
🔧 Programmierung

🍏 Python Runner 1.5.3 - Write and run Python code instantly.


📈 15.87 Punkte
🍏 iOS / Mac OS

🐧 Python Compile: Python-Code mithilfe einer Funktion ausführen


📈 15.87 Punkte
🐧 Server

🕵️ 15-Year-Old Python Bug Let Hacker Execute Code in 350k Python Projects


📈 15.87 Punkte
🕵️ Hacking

🎥 Getting Started with Python in VS Code (Official Video)


📈 14.3 Punkte
🎥 Video | Youtube

🎥 Getting Started with Python in VS Code (Official Video)


📈 14.3 Punkte
🎥 Video | Youtube

matomo