Phản hồi
Gửi và xem ý kiến phản hồi dành cho
Python in Visual Studio Code
Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code’s power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.
This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.
Download and install the Python workload
Complete the following steps to download and install the Python workload.
-
Download and run the latest Visual Studio Installer for Windows. Python support is present in release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting Tools > Get Tools and Features.
Tip
The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio Professional or Visual Studio Enterprise.
-
The installer provides a list of workloads that are groups of related options for specific development areas. For Python, select the Python development workload and select Install:
Python installation options Description Python distributions Choose any combination of Python distribution that you plan to work with. Common options include 32-bit and 64-bit variants of Python 2, Python 3, Miniconda, Anaconda 2, and Anaconda 3. Each option includes the distribution’s interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of preinstalled packages. Visual Studio automatically detects existing Python installations. For more information, see The Python Environments window. Also, if a newer version of Python is available than the version shown in the installer, you can install the new version separately and Visual Studio detects it. Cookiecutter template support Install the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. For more information, see Use the Cookiecutter extension. Python web support Install tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. For more information, see Python web project templates. Python native development tools Install the C++ compiler and other necessary components to develop native extensions for Python. For more information, see Create a C++ extension for Python. Also install the Desktop development with C++ workload for full C++ support. By default, the Python workload installs for all users on a computer under:
%ProgramFiles%\Microsoft Visual Studio\
\
Common7\IDE\Extensions\Microsoft\Python
where
is 2022 and
is Community, Professional, or Enterprise.
%ProgramFiles(x86)%\Microsoft Visual Studio\
\
Common7\IDE\Extensions\Microsoft\Python
where
is 2019 or 2017 and
is Community, Professional, or Enterprise.
Version Control integration
Collaborate on code with Git
Use Git as the default source control experience in Visual Studio right out of the box. From the new Git menu, you can create or clone repositories from GitHub or Azure DevOps. Use the integrated Git tool windows to commit and push changes to your code, manage branches, sync with your remote repositories, and resolve merge conflicts.
Environments
The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments.
The current environment is shown on the right side of the VS Code Status Bar:
The Status Bar also indicates if no interpreter is selected:
The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature. It is also activated when you run or debug Python in a terminal, or when you create a new terminal with the Terminal: Create New Terminal command.
To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.
VS Code prompts you with a list of detected environments as well as any you’ve added manually to your user settings (see Configuring Python environments).
Feature details
Learn more about the rich features of the Python extension:
- IntelliSense: Edit your code with auto-completion, code navigation, syntax checking and more
- Linting: Get additional code analysis with Pylint, Flake8 and more
- Code formatting: Format your code with black, autopep or yapf
- Debugging: Debug your Python scripts, web apps, remote or multi-threaded processes
- Testing: Run and debug tests through the Test Explorer with unittest or pytest.
- Jupyter Notebooks: Create and edit Jupyter Notebooks, add and run code cells, render plots, visualize variables through the variable explorer, visualize dataframes with the data viewer, and more
- Environments: Automatically activate and switch between virtualenv, venv, pipenv, conda and pyenv environments
- Refactoring: Restructure your Python code with variable extraction and method extraction. Additionally, there is componentized support to enable additional refactoring, such as import sorting, through extensions including isort and Ruff.
Data and telemetry
The Microsoft Python Extension for Visual Studio Code collects usage
data and sends it to Microsoft to help improve our products and
services. Read our
privacy statement to
learn more. This extension respects the
telemetry.enableTelemetry
setting which you can learn more about at
https://code.visualstudio.com/docs/supporting/faq#_how-to-disable-telemetry-reporting.
[!Tip]\nThe Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio Professional or Visual Studio Enterprise .
Download and run the latest Visual Studio Installer for Windows. Python support is present in release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting Tools > Get Tools and Features .
The installer provides a list of workloads that are groups of related options for specific development areas. For Python, select the
Python development
workload and select
Install
:
\n
:::image type=\”content\” source=\”media/installation-python-workload.png\” alt-text=\”Screenshot of the Python development workload selected in the Visual Studio installer.\” lightbox=\”media/installation-python-workload.png\”:::
\n\n\n\nPython installation options\nDescription\n\n\n\n\n
Python distributions
\nChoose any combination of Python distribution that you plan to work with. Common options include 32-bit and 64-bit variants of Python 2, Python 3, Miniconda, Anaconda 2, and Anaconda 3. Each option includes the distribution’s interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of preinstalled packages. Visual Studio automatically detects existing Python installations. For more information, see
The Python Environments window
. Also, if a newer version of Python is available than the version shown in the installer, you can install the new version separately and Visual Studio detects it.\n\n\n
Cookiecutter template support
\nInstall the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. For more information, see
Use the Cookiecutter extension
.\n\n\n
Python web support
\nInstall tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. For more information, see
Python web project templates
.\n\n\n
Python native development tools
\nInstall the C++ compiler and other necessary components to develop native extensions for Python. For more information, see
Create a C++ extension for Python
. Also install the
Desktop development with C++
workload for full C++ support.\n\n\n\n
By default, the Python workload installs for all users on a computer under:
\n
:::moniker range=\”>=vs-2022\”
\n
%ProgramFiles%\\Microsoft Visual Studio\\
\\
Common7\\IDE\\Extensions\\Microsoft\\Python
\n
where
is 2022 and
is Community, Professional, or Enterprise.
\n
:::moniker-end\n:::moniker range=\”<=vs-2019\”
\n
%ProgramFiles(x86)%\\Microsoft Visual Studio\\
\\
Common7\\IDE\\Extensions\\Microsoft\\Python
\n
where
is 2019 or 2017 and
is Community, Professional, or Enterprise.
\n
:::moniker-end
\n
\n
[!Tip]\nThe Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio Professional or Visual Studio Enterprise .
Download and run the latest Visual Studio Installer for Windows. Python support is present in release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting Tools > Get Tools and Features .
The installer provides a list of workloads that are groups of related options for specific development areas. For Python, select the
Python development
workload and select
Install
:
\n
:::image type=\”content\” source=\”media/installation-python-workload.png\” alt-text=\”Screenshot of the Python development workload selected in the Visual Studio installer.\” lightbox=\”media/installation-python-workload.png\”:::
\n\n\n\nPython installation options\nDescription\n\n\n\n\n
Python distributions
\nChoose any combination of Python distribution that you plan to work with. Common options include 32-bit and 64-bit variants of Python 2, Python 3, Miniconda, Anaconda 2, and Anaconda 3. Each option includes the distribution’s interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of preinstalled packages. Visual Studio automatically detects existing Python installations. For more information, see
The Python Environments window
. Also, if a newer version of Python is available than the version shown in the installer, you can install the new version separately and Visual Studio detects it.\n\n\n
Cookiecutter template support
\nInstall the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. For more information, see
Use the Cookiecutter extension
.\n\n\n
Python web support
\nInstall tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. For more information, see
Python web project templates
.\n\n\n
Python native development tools
\nInstall the C++ compiler and other necessary components to develop native extensions for Python. For more information, see
Create a C++ extension for Python
. Also install the
Desktop development with C++
workload for full C++ support.\n\n\n\n
By default, the Python workload installs for all users on a computer under:
\n
:::moniker range=\”>=vs-2022\”
\n
%ProgramFiles%\\Microsoft Visual Studio\\
\\
Common7\\IDE\\Extensions\\Microsoft\\Python
\n
where
is 2022 and
is Community, Professional, or Enterprise.
\n
:::moniker-end\n:::moniker range=\”<=vs-2019\”
\n
%ProgramFiles(x86)%\\Microsoft Visual Studio\\
\\
Common7\\IDE\\Extensions\\Microsoft\\Python
\n
where
is 2019 or 2017 and
is Community, Professional, or Enterprise.
\n
:::moniker-end
\n
\n
Install and configure Visual Studio Code for Python development
Install and configure Visual Studio Code to create a development environment for learning to build Python applications.
Next steps
- Python Hello World tutorial – Get started with Python in VS Code.
- Editing Python – Learn about auto-completion, formatting, and refactoring for Python.
- Basic Editing – Learn about the powerful VS Code editor.
- Code Navigation – Move quickly through your source code.
- Django tutorial
- Flask tutorial
Develop Python applications
Debugging
No more
For more specific information on debugging in Python, such as configuring your
launch.json
settings and implementing remote debugging, see Debugging. General VS Code debugging information is found in the debugging document.
Additionally, the Django and Flask tutorials provide examples of how to implement debugging in the context of web applications, including debugging Django templates.
Questions, issues, feature requests, and contributions
- If you have a question about how to accomplish something with the extension, please ask on Stack Overflow
- If you come across a problem with the extension, please file an issue
- Contributions are always welcome! Please see our contributing guide for more details
-
Any and all feedback is appreciated and welcome!
- If someone has already filed an issue that encompasses your feedback, please leave a 👍/👎 reaction on the issue
- Otherwise please start a new discussion
- If you’re interested in the development of the extension, you can read about our development process
Useful commands
Open the Command Palette (Command+Shift+P on macOS and Ctrl+Shift+P on Windows/Linux) and type in one of the following commands:
Command | Description |
|
Switch between Python interpreters, versions, and environments. |
|
Start an interactive Python REPL using the selected interpreter in the VS Code terminal. |
|
Runs the active Python file in the VS Code terminal. You can also run a Python file by right-clicking on the file and selecting
. |
|
Formats code using the provided formatter in the
file. |
|
Select a test framework and configure it to display the Test Explorer. |
To see all available Python commands, open the Command Palette and type
Python
. For Jupyter extension commands, just type
Jupyter
.
Installed extensions
The Python extension will automatically install the following extensions by default to provide the best Python development experience in VS Code:
- Pylance – to provide performant Python language support
- Python Debugger – to provide a seamless debug experience with debugpy
These extensions are optional dependencies, meaning the Python extension will remain fully functional if they fail to be installed. Any or all of these extensions can be disabled or uninstalled at the expense of some features. Extensions installed through the marketplace are subject to the Marketplace Terms of Use.
Testing
The Python extension supports testing with Python’s built-in unittest framework and pytest.
In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.
Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods
Supported locales
The extension is available in multiple languages:
de
,
en
,
es
,
fa
,
fr
,
it
,
ja
,
ko-kr
,
nl
,
pl
,
pt-br
,
ru
,
tr
,
zh-cn
,
zh-tw
Open source
Fork us on Github
Python Tools for Visual Studio is a completely free extension, developed and supported by Microsoft with contributions from the community. Visit our Github page to see or participate in PTVS development.
Python extension for Visual Studio Code
A Visual Studio Code extension with rich support for the Python language (for all actively supported versions of the language: >=3.7), including features such as IntelliSense (Pylance), linting, debugging (Python Debugger), code navigation, code formatting, refactoring, variable explorer, test explorer, and more!
The Python extension does offer some support when running on vscode.dev (which includes github.dev). This includes partial IntelliSense for open files in the editor.
Install Python and the Python extension
The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace.
Note: To help get you started with Python development, you can use the Python profile template that includes useful extensions, settings, and Python code snippets.
Once you have a version of Python installed, select it using the Python: Select Interpreter command. If VS Code doesn’t automatically locate the interpreter you’re looking for, refer to Environments – Manually specify an interpreter.
You can configure the Python extension through settings. Learn more in the Python Settings reference.
Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.
Enhance completions with AI
GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.
GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++.
You can learn more about how to get started with Copilot in the Copilot documentation.
Wondering which tool is best for you? We can help
Visual Studio Code for Mac
Highlights
- Free code editor
- Built on open source and runs everywhere
-
Hundreds of programming languages supported
-
Add on the C# DevKit for Visual Studio Code
Visual Studio Code for Windows
Lightweight yet powerful source code editor with tons of
extensions for many languages and runtimes.Download Visual Studio Code
Highlights
- Free code editor
- Built on open source and runs everywhere
-
Hundreds of programming languages supported
Visual Studio Code for Mac
Highlights
- Free code editor
- Built on open source and runs everywhere
-
Hundreds of programming languages supported
Visual Studio Code for Linux
Highlights
- Free code editor
- Built on open source. Runs everywhere
-
Hundreds of programming languages supported
Visual Studio for Windows
A complete array of development tools and features in one
place to elevate and enhance every stage of your
software development.How to install offline Compare editions
Highlights
- Free for individual use
- Code faster, test, debug, deploy any app from one place
-
Visual Studio built-in features empower full development cycle.
Test your install
Quickly check your installation of Python support:
-
Launch Visual Studio.
-
Select Alt + I to open the Python Interactive window.
-
In the window, enter the statement
2+2
.The statement output
displays in the window. If you don’t see the correct output, recheck your steps.
Autocomplete and IntelliSense
The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.
IntelliSense quickly shows methods, class members, and documentation as you type. You can also trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space). Hovering over identifiers will show more information about them.
Run Python code
To experience Python, create a file (using the File Explorer) named
hello.py
and paste in the following code:
print("Hello World")
The Python extension then provides shortcuts to run Python code using the currently selected interpreter (Python: Select Interpreter in the Command Palette). To run the active Python file, click the Run Python File in Terminal play button in the top-right side of the editor.
You can also run individual lines or a selection of code with the Python: Run Selection/Line in Python Terminal command (Shift+Enter). If there isn’t a selection, the line with your cursor will be run in the Python Terminal. An identical Run Selection/Line in Python Terminal command is available on the context menu for a selection in the editor. The same terminal will be used every time you run a selection or a line in the terminal/REPL, until that terminal is closed. The same terminal is also used for Run Python File in Terminal. If that terminal is still running the REPL, you should exit the REPL (
exit()
) or switch to a different terminal before running a Python file.
The Python extension automatically removes indents based on the first non-empty line of the selection, shifting all other lines left as needed.
The command opens the Python Terminal if necessary; you can also open the interactive REPL environment directly using the Python: Start REPL command that activates a terminal with the currently selected interpreter and then runs the Python REPL.
For a more specific walkthrough and other ways of running code, see the run code tutorial.
Python profile template
Profiles let you quickly switch your extensions, settings, and UI layout depending on your current project or task. To help you get started with Python development, you can use the Python profile template, which is a curated profile with useful extensions, settings, and snippets. You can use the profile template as is or use it as a starting point to customize further for you own workflows.
You select a profile template through the Profiles > Create Profile… dropdown:
Once you select a profile template, you can review the settings and extensions, and remove individual items if you don’t want to include them in your new Profile. After creating the new profile based on the template, changes made to settings, extensions, or UI are persisted in your profile.
Other popular Python extensions
The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.
- Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- Filter the extension list by typing ‘python’.
The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.
Jupyter notebooks
To enable Python support for Jupyter notebook files (
.ipynb
) in VS Code, you can install the Jupyter extension. The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.
You can also convert and open the notebook as a Python code file through the Jupyter: Export to Python Script command. The notebook’s cells are delimited in the Python file with
#%%
comments, and the Jupyter extension shows Run Cell or Run Below CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:
You can also connect to a remote Jupyter server to run your notebooks. For more information, see Jupyter support.
All Downloads
Sử dụng Visual Studio Code Python
Python đã trở thành một trong những ngôn ngữ lập trình phổ biến năm 2022.
Bài viết này sẽ hướng dẫn các bạn sử dụng Visual Studio Code – Một Editor đa năng phát triển bởi MicroSoft.
Để cài đặt
Các bạn download từ link bên dưới (Link chính thức của Microsoft) DOWNLOAD VISUAL STUDIO CODE
Việc cài đặt rất dễ dàng, bạn chọn file cài đặt tương ứng với hệ điều hành sử dụng, click đúp cài đặt phần mềm.
Các extension hỗ trợ lập trình
Visual Studio Code được Microsoft phát triển cho nhiều ngôn ngữ lập trình, nên để lập trình Python trên đó các bạn cài một số extension cần thiết.
Để cài extension bằng lệnh, trên VS code bấm tổ hợp phím [ Ctrl + P ], nhập lệnh cài đặt và gõ phím [ Enter ]
Để cài đặt thông thường các bạn bấm tổ hợp phím [ Ctrl + Shift + X ] hoặc bấm vào biểu tượng Extension trên VS code, tìm kiếm extension cần thiết bấm [ Install ] để cài đặt.
Hướng dẫn sử dụng VS
Tạo Workspace
Từ cửa sổ VS Code, bấm tổ hợp phím [ Ctrl + N ]
Tạo file hello-world.py
Lần sau bạn muốn mở lại Project chỉ cần chọn [ Ctrl + O ] browser tới file này.
Chạy python script
Sau khi tạo file hello-world.py, để chạy file này chúng ta kích chuột phải vào file chọn “Run python file in terminal”
Kết quả:
Một số mẹo hay khi lập trình Python bằng VS
a. Nhảy tới 1 function
Giữ phím [ Ctrl ] và bấm vào function, method để nhảy tới function mà bạn đã định nghĩa.
b. Format source code theo chuẩn PEP 8
python -m pip install -U autopep8 --user
Bấm tổ hợp phím [ Ctrl + Shift + I ] để format file source code cho chúng.
All rights reserved
Keywords searched by users: download visual studio for python
Categories: Cập nhật 30 Download Visual Studio For Python
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/