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 configurepython.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 seteditor.formatOnSave
tofalse
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!