search
HomeDevelopment ToolsVSCodeHow to use vscode for python programming

How to use vscode for python programming

Dec 12, 2019 pm 04:16 PM
pythonvscodeprogramming

How to use vscode for python programming

Install the Python extension in VS Code

First of all, in order to make Python development in VS Code more convenient, you need to extend it from VS Code Install Python extensions in the store.

After the Python extension is installed, you can start configuring the Python extension.

VS Code manages settings through two JSON files:

One file is used for global settings of VS Code, which affects all projects; the other file is used for special settings, which affects individual projects. You can use the shortcut key Ctrl , (comma) to open global settings.

Set the Python path

You can configure python.pythonPath in the global settings to make VS Code automatically select the most suitable Python for each project interpreter.

// 将设置放在此处以覆盖默认设置和用户设置。
// Path to Python, you can use a custom version of Python by modifying this setting to include 
the full path.
{    
"python.pythonPath":"${workspaceRoot}/.venv/bin/python",
}

In this way, VS Code will use the Python interpreter in the project root directory under the virtual environment directory .venv.

Using environment variables

By default, VS Code uses the environment variables defined in the .env file in the project root directory. This is useful for setting environment variables such as:

PYTHONWARNINGS="once"

to cause the program to display warnings while running.

You can load other default environment variable files by setting python.envFile:

// Absolute path to a file containing environment variable definitions.
"python.envFile": "${workspaceFolder}/.env",

Code analysis

Python extension also supports different code analysis Tools (pep8, flake8, pylint). To enable analysis tools that you like or are using for projects you are working on, all it takes is a few simple configurations.

The extension uses pylint by default for code analysis. You can configure it like this to use flake8 for analysis:

"python.linting.pylintEnabled": false,
"python.linting.flake8Path": "${workspaceRoot}/.venv/bin/flake8",
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": ["--max-line-length=90"],

After enabling code analysis, the analyzer will add wavy lines at positions that do not meet the requirements. When the mouse is placed at this position, a pop-up window will prompt the reason. Note that flake8 needs to be installed in the project's virtual environment for this example to be valid.

Formatting code

You can configure VS Code to automatically format code. Currently supports autopep8, black and yapf. The settings below will enable "black" mode.

// Provider for formatting. Possible options include 'autopep8', 'black', and 'yapf'.
"python.formatting.provider": "black",
"python.formatting.blackPath": "${workspaceRoot}/.venv/bin/black"
"python.formatting.blackArgs": ["--line-length=90"],"editor.formatOnSave": true,

If you do not need the editor to automatically format the code when saving, you can set editor.formatOnSave to false and manually use the shortcut key Ctrl Shift I to format Code in the current document.

Note that black needs to be installed in the virtual environment of the project for this example to be valid.

Run tasks

An important feature of VS Code is that it can run tasks. The tasks that need to be run are saved in a JSON file in the project root directory.

Running the flask development server

This example will create a task to run the Flask development server. Use a basic template that can run external commands to create a new project:

Edit the tasks.json file as shown below and create a new task to run the Flask development service:

{    // See https://go.microsoft.com/fwlink/?LinkId=733558    
// for the documentation about the tasks.json format    
"version": "2.0.0",    
"tasks": [    
{     
"label": "Run Debug Server",    
"type": "shell",    
"command": "${workspaceRoot}/.venv/bin/flask run -h 0.0.0.0 -p 5000",    
"group": {    
"kind": "build",    
"isDefault": true   
}    
}    
]
}

Flask development service Use environment variables to obtain the entry point of your application. As mentioned in the Using Environment Variables section, you can declare these variables in the .env file:

FLASK_APP=wsgi.py
FLASK_DEBUG=True

Then you can use the shortcut key Ctrl Shift B to perform tasks.

Unit testing

VS Code also supports unit testing frameworks pytest, unittest and nosetest. After enabling the test framework, you can run the searched unit tests individually, run the tests through a test suite, or run all tests in VS Code.

For example, you can enable the pytest testing framework like this:

"python.unitTest.pyTestEnabled": true,
"python.unitTest.pyTestPath": "${workspaceRoot}/.venv/bin/pytest",

Note: pytest needs to be installed in the virtual environment of the project for this example to be valid.

Recommended related articles and tutorials: vscode tutorial

The above is the detailed content of How to use vscode for python programming. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Visual Studio Professional and Enterprise: Paid Versions and FeaturesVisual Studio Professional and Enterprise: Paid Versions and FeaturesMay 10, 2025 am 12:20 AM

The difference between VisualStudioProfessional and Enterprise is in the functionality and target user groups. The Professional version is suitable for professional developers and provides functions such as code analysis; the Enterprise version is for large teams and has added advanced tools such as test management.

Choosing Between Visual Studio and VS Code: The Right Tool for YouChoosing Between Visual Studio and VS Code: The Right Tool for YouMay 09, 2025 am 12:21 AM

VisualStudio is suitable for large projects, VSCode is suitable for projects of all sizes. 1. VisualStudio provides comprehensive IDE functions, supports multiple languages, integrated debugging and testing tools. 2.VSCode is a lightweight editor that supports multiple languages ​​through extension, has a simple interface and fast startup.

Visual Studio: A Powerful Tool for DevelopersVisual Studio: A Powerful Tool for DevelopersMay 08, 2025 am 12:19 AM

VisualStudio is a powerful IDE developed by Microsoft, supporting multiple programming languages ​​and platforms. Its core advantages include: 1. Intelligent code prompts and debugging functions, 2. Integrated development, debugging, testing and version control, 3. Extended functions through plug-ins, 4. Provide performance optimization and best practice tools to help developers improve efficiency and code quality.

Visual Studio vs. VS Code: Pricing, Licensing, and AvailabilityVisual Studio vs. VS Code: Pricing, Licensing, and AvailabilityMay 07, 2025 am 12:11 AM

The differences in pricing, licensing and availability of VisualStudio and VSCode are as follows: 1. Pricing: VSCode is completely free, while VisualStudio offers free community and paid enterprise versions. 2. License: VSCode uses a flexible MIT license, and the license of VisualStudio varies according to the version. 3. Usability: VSCode is supported across platforms, while VisualStudio performs best on Windows.

Visual Studio: From Code to ProductionVisual Studio: From Code to ProductionMay 06, 2025 am 12:10 AM

VisualStudio supports the entire process from code writing to production deployment. 1) Code writing: Provides intelligent code completion and reconstruction functions. 2) Debugging and testing: Integrate powerful debugging tools and unit testing framework. 3) Version control: seamlessly integrate with Git to simplify code management. 4) Deployment and Release: Supports multiple deployment options to simplify the application release process.

Visual Studio: A Look at the Licensing LandscapeVisual Studio: A Look at the Licensing LandscapeMay 05, 2025 am 12:17 AM

VisualStudio offers three license types: Community, Professional and Enterprise. The Community Edition is free, suitable for individual developers and small teams; the Professional Edition is annually subscribed, suitable for professional developers who need more functions; the Enterprise Edition is the highest price, suitable for large teams and enterprises. When selecting a license, project size, budget and teamwork needs should be considered.

The Ultimate Showdown: Visual Studio vs. VS CodeThe Ultimate Showdown: Visual Studio vs. VS CodeMay 04, 2025 am 12:01 AM

VisualStudio is suitable for large-scale project development, while VSCode is suitable for projects of all sizes. 1. VisualStudio provides comprehensive development tools, such as integrated debugger, version control and testing tools. 2.VSCode is known for its scalability, cross-platform and fast launch, and is suitable for fast editing and small project development.

Visual Studio vs. VS Code: Comparing the Two IDEsVisual Studio vs. VS Code: Comparing the Two IDEsMay 03, 2025 am 12:04 AM

VisualStudio is suitable for large projects and Windows development, while VSCode is suitable for cross-platform and small projects. 1. VisualStudio provides a full-featured IDE, supports .NET framework and powerful debugging tools. 2.VSCode is a lightweight editor that emphasizes flexibility and extensibility, and is suitable for various development scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools