Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VS Code for Python programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use VS Code for Python programming, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Visual Studio Code, or VS Code for short, is an open source text editor that contains tools for building and debugging applications. After installing and enabling the Python extension, VS Code can be configured as an ideal working environment for Python development. This article introduces some useful VS Code extensions and configures them to fully improve the efficiency of Python development.

If you don't already have VS Code installed on your computer, you can refer to the article to install it on Fedora using VS Code.

Install the Python extension in VS Code

First, to make it easier to develop Python in VS Code, you need to install the Python extension from the VS Code extension store.

After the Python extension installation is complete, you can begin to configure the Python extension.

VS Code manages settings through two JSON files:

One file is used for the global settings of VS Code and works on all projects

Another file is used for special settings and works on individual projects

You can use the shortcut key Ctrl+, (comma) to open the global settings, or you can open them through the file-> * item-> settings.

Set the Python path

You can configure python.pythonPath in the global settings so that VS Code automatically selects the most appropriate Python interpreter for each project.

/ / put the settings here to override the default and user settings. / / 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.

Use environment variables

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

PYTHONWARNINGS= "once"

Causes the program to display warnings at run time.

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

The Python extension also supports different code analysis tools (pep8, flake8, pylint). To enable the analysis tools used by your favorite or ongoing projects, you only need to do some simple configuration.

Extensions use pylint for code analysis by default. You can configure it 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"]

When code analysis is enabled, the analyzer adds a wavy line to a location that does not meet the requirements, where the mouse is positioned to indicate the reason. Note that flake8 needs to be installed in the virtual environment of the project for this example to be valid.

Formatting code

You can configure VS Code to format the code automatically. Autopep8, black, and yapf are currently supported. The following settings 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 at save time, you can set editor.formatOnSave to false and manually format the code in the current document using the shortcut key Ctrl + Shift + I. Note that black needs to be installed in the virtual environment of the project for this example to be valid.

Run the task

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

Run the flask development service

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

Edit the tasks.json file shown below to 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}]}

The Flask development service uses environment variables to obtain the entry point of the application. As mentioned in the section using environment variables, you can declare these variables in the .env file:

FLASK_APP=wsgi.pyFLASK_DEBUG=True

This allows you to use the shortcut key Ctrl + Shift + B to perform the task.

Unit testing

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

For example, you can enable the pytest test framework as follows:

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

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

The above is how to use VS Code for Python programming. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report