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.
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
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.
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.
Downloads
Prerequisites
-
Visual Studio supports Python version 3.7. While it’s possible to use an earlier version of Visual Studio to edit code written in earlier versions of Python, those versions of Python aren’t officially supported. Visual Studio features such as IntelliSense and debugging might not work with earlier versions of Python.
-
For Visual Studio 2015 and earlier, use Python 3.5 or earlier. You must manually install one of the Python interpreters.
Anaconda distributions
Although Visual Studio offers to install the Anaconda distribution, your use of the distribution and other packages from Anaconda Repository are bound by the Anaconda Terms of Service. These terms might require some organizations to pay Anaconda for a commercial license, or else configure the tools to access an alternate repository. For more information, see the Conda channels documentation.
Move an interpreter
If you move an existing interpreter to a new location by using the file system, Visual Studio doesn’t automatically detect the change.
-
If you originally specified the location of the interpreter through the Python Environments window, you can edit its environment by using the Configure tab in that window to identify the new location. For more information, see Manually identify an existing environment.
-
If you installed the interpreter by using an installer program, use the following steps to reinstall the interpreter in the new location:
- Restore the Python interpreter to its original location.
- Uninstall the interpreter by using its installer, which clears the registry entries.
- Reinstall the interpreter at the new location.
- Restart Visual Studio, which should autodetect the new location in place of the old location.
This process ensures that the registry entries that identify the interpreter’s location, which Visual Studio uses, are properly updated. Using an installer also handles any other side effects that might exist.
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.
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.
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.
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
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.
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.
Detect your environment
Visual Studio shows all known environments in the Python Environments window. It automatically detects updates to existing interpreters.
If Visual Studio doesn’t detect an installed environment, see Manually identify an existing environment.
If you want to provide new forms of detection for Python environments, see PTVS Environment Detection (github.com).
Registry entries
Visual Studio (all versions) automatically detects each installed Python interpreter and its environment by checking the registry according to PEP 514 – Python registration in the Windows registry. Python installations are typically found under the HKEY_LOCAL_MACHINE\SOFTWARE\Python (32-bit) and HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Python (64-bit) key within nodes for the distribution, such as PythonCore (CPython) and ContinuumAnalytics (Anaconda).
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.
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
.
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
All Downloads
Install Python interpreters
There are several options for installing Python interpreters to work with Visual Studio. You can install an interpreter when you install the Python workload, or you can install an interpreter after a workload is present. Interpreters can also be installed manually outside of the Visual Studio Installer.
When you install the Python development workload in Visual Studio 2017 and later, Python 3 (64-bit) also installs by default. As an option, you can choose to install the 32-bit or 64-bit version of Python 2 or Python 3, along with Miniconda (Visual Studio 2019) or Anaconda 2/Anaconda 3 (Visual Studio 2017). The steps for this type of installation are described in Install Python support in Visual Studio.
An alternate approach is to install standard Python interpreters by using the Add Environment feature in Visual Studio. This option is available in the Python Environments window and the Python toolbar.
Python interpreters can also be installed manually outside of the Visual Studio Installer. Suppose you install Anaconda 3 before you install Visual Studio. You don’t need to reinstall Anaconda through the Visual Studio Installer. You can also install a newer version of an interpreter if it isn’t yet listed in the Visual Studio Installer.
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.
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.
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.
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.
Feedback
Submit and view feedback for
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
Review Python interpreters
The following table lists Python interpreters that can be used with Visual Studio.
Interpreter | Description | Notes |
CPython | The “native” and most commonly used interpreter, available in 32-bit and 64-bit versions (32-bit recommended). Includes the latest language features, maximum Python package compatibility, full debugging support, and interop with IPython. Review the considerations in Should I use Python 2 or Python 3? to help determine which version of Python to install. | Visual Studio 2015 and earlier don’t support Python 3.6 or later, and can return errors like Unsupported python version 3.6. For Visual Studio 2015 and earlier, use Python 3.5 or earlier. |
IronPython | A .NET implementation of Python, available in 32-bit and 64-bit versions. Provides C#/F#/Visual Basic interop, access to .NET APIs, standard Python debugging (but not C++ mixed-mode debugging), and mixed IronPython/C# debugging. | IronPython doesn’t support virtual environments. |
Anaconda | An open data science platform powered by Python. Includes the latest version of CPython and most of the difficult-to-install packages. | If you’re unable to decide on an interpreter, we recommend using Anaconda. |
PyPy | A high-performance tracing JIT implementation of Python. Good for long-running programs and situations where you identify performance issues but can’t find other resolutions. | Works with Visual Studio but with limited support for advanced debugging features. |
Jython | An implementation of Python on the Java Virtual Machine (JVM). Similar to IronPython, code running in Jython can interact with Java classes and libraries. However, many of the libraries intended for CPython might not be accessible. | Works with Visual Studio but with limited support for advanced debugging features. |
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.
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.
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.
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).
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.
Keywords searched by users: visual studio download python
Categories: Cập nhật 92 Visual Studio Download Python
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/