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.
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).
Modify the registry to correct an environment
If the Python environment doesn’t have a repair option, or you want to remove an invalid environment, you can use the following steps to modify the registry directly. Visual Studio automatically updates thePython Environments window when you make changes to the registry.
-
Run the regedit.exe executable to open the Registry editor.
-
Browse to the environment folder that corresponds to your configuration:
Python version Folder 64-bit version HKEY_LOCAL_MACHINE\SOFTWARE\Python or HKEY_CURRENT_USER\Software\Python 32-bit version HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Python IronPython IronPython -
Expand the distribution and version node structure for your environment:
Distribution Node CPython PythonCore > Anaconda ContinuumAnalytics > IronPython -
Inspect the values under the InstallPath node:
- If the environment still exists on your computer, change the value of the ExecutablePath entry to the correct location. Also correct the values for the (Default) and WindowedExecutablePath entries, as necessary.
- If the environment no longer exists on your computer and you want to remove it from the Python Environments window, delete the version number parent node of the InstallPath node. You can see an example of this node in the preceding image. In the example, this node is 3.6.
Caution
Invalid settings in the HKEY_CURRENT_USER\SOFTWARE\Python key override the settings in the HKEY_LOCAL_MACHINE\SOFTWARE\Python key.
The Python Environments window
The environments that Visual Studio knows about are displayed in the Python Environments window. To open the window, use one of the following methods:
- Select View > Other Windows > Python Environments.
- Right-click the Python Environments node for a project in Solution Explorer and select View All Python Environments.
The Python Environments window appears alongside Solution Explorer in Visual Studio:
Visual Studio looks for installed global environments by using the registry (following PEP 514), along with virtual environments and conda environments (see Types of environments). If you don’t see an expected environment in the list, see Manually identify an existing environment.
When you select an environment in the list, Visual Studio displays various properties and commands for that environment on the Overview tab of the Python Environments window, such as the interpreter location. The commands at the bottom of the Overview tab each open a command prompt with the interpreter running. For more information, see Python Environments window tab reference – Overview.
Use the dropdown list under the list of environments to switch to different tabs such as Packages and IntelliSense. These tabs are also described in the Python Environments window tab reference.
Selecting an environment doesn’t change its relation to any projects. The default environment, shown in boldface in the list, is the one that Visual Studio uses for any new projects. To use a different environment with new projects, use the Make this the default environment for new projects command. Within the context of a project, you can always select a specific environment. For more information, see Select an environment for a project.
To the right of each listed environment, is a control that opens an Interactive window for that environment. (In Visual Studio 2017 15.5 and earlier, another control appears that refreshes the IntelliSense database for that environment. For details about the database, see Python Environments window tab reference.)
Note
Although Visual Studio respects the system-site-packages option, it doesn’t provide a way to change it from within Visual Studio.
What if no environments appear?
If you don’t see any environments in the Python Environments window, it means Visual Studio failed to detect any Python installations in standard locations. Maybe you installed Visual Studio 2017 or later but cleared all the interpreter options in the installer options for the Python workload. Similarly, you possibly installed Visual Studio 2015 or earlier but didn’t install an interpreter manually. For more information, see Install Python interpreters.
If you know you have a Python interpreter on your computer but Visual Studio (any version) didn’t detect it, use the + Custom command to specify the interpreter location manually. For more information, see how to manually identify an existing environment.
Types of environments
Visual Studio can work with global, virtual, and conda environments.
Global environments
Each Python installation maintains its own global environment. For example, Python 2.7, Python 3.6, Python 3.7, Anaconda 4.4.0, and so on. For more information, see Install Python interpreters.
Each environment is composed of the specific Python interpreter, its standard library, and a set of preinstalled packages. It also contains any other packages you install while the environment is activated. Installing a package into a global environment makes it available to all projects using that environment. If the environment is located in a protected area of the file system (within c:\program files, for example), then installing packages requires administrator privileges.
Global environments are available to all projects on the computer. In Visual Studio, you select one global environment as the default, which is used for all projects unless you specifically choose a different one for a project. For more information, see Select an environment for a project.
Virtual environments
Working in a global environment is an easy way to get started. Over time, environments can become cluttered with many different packages installed for different projects. The clutter can make it difficult to thoroughly test your application against a specific set of packages with known versions. But this kind of environment is what you would expect to set up on a build server or web server. Conflicts can also occur when two projects require incompatible packages or different versions of the same package.
For these reasons, developers often create a virtual environment for a project. A virtual environment is a subfolder in a project that contains a copy of a specific interpreter. If you activate the virtual environment, any packages you install are installed only in that environment’s subfolder. When you run a Python program within the virtual environment, you can be confident that the program is running against only those specific packages.
Visual Studio provides direct support for creating a virtual environment for a project. If you open a project that contains a requirements.txt file, Visual Studio prompts you automatically to create a virtual environment and install those dependencies. You see the same behavior when you create a project from a template that includes requirements.txt file.
At any time within an open project, you can create a new virtual environment. In Solution Explorer, expand the project node, right-click Python Environments, and choose Add environment. In Add Environment, choose Virtual environment. For more information, see Create a virtual environment.
Visual Studio also provides a command to generate a requirements.txt file from a virtual environment, making it easy to recreate the environment on other computers. For more information, see Use virtual environments.
Conda environments
You can create a conda environment by using the
conda
tool, or with integrated conda management in Visual Studio 2017 version 15.7 and later. A conda environment requires Anaconda or Miniconda. These platforms are available through the Visual Studio Installer. For more information, see Install Python support in Visual Studio.
-
In the Python Environments window (or from the Python toolbar), select Add Environment to open the Add environment dialog.
-
In the Add environment dialog, select the Conda environment tab:
-
Configure the following fields:
Field Description Project Identifies the project in which to create the environment. Name Provides the name for the conda environment. Add packages from Specifies how to add packages to the conda environment. – Environment file: Select this option if you have an environment.yml file that describes your dependencies. Enter the name of the file or browse (…) to the file location and select the file.- One or more Anaconda package names: Select this option if you want to list one or more Python packages or Python versions.The package list instructs conda to create a Python environment. To install the latest version of Python, use the
python
command. To install a specific version, use the command
python=,major>.
as in
python=3.7
. You can also use the package button to select Python versions and common packages from a series of menus.Set as current environment Activates the new environment in the selected project after the environment is created. Set as default environment for new projects Automatically sets and activates the conda environment in any new projects created in Visual Studio. This option is the same as using the Make this the default environment for new projects in the Python Environments window. View in Python Environments window Specifies whether to show the Python Environments window after creating the environment. Important
When you create a conda environment, be sure to specify at least one Python version or Python package to ensure the environment contains a Python runtime. You can use a
environments.yml
file or the package list. If you don’t provide this specification, Visual Studio ignores the environment. The environment doesn’t appear anywhere in the Python Environments window, it’s not set as the current environment for a project, and it’s not available as a global environment.If you happen to create a conda environment without a Python version, use the
conda info
command to see the locations of conda environment folders. You can then manually remove the subfolder for the environment from that location. -
Select Create.
You can monitor creation of the conda environment in the Output window. After creation completes, the output displays some command-line interface (CLI) instructions, such as
activate env
: -
In Visual Studio, you can activate a conda environment for your project in the same way that you would any other environment. For more information, see Select an environment for a project.
-
To install more packages in the environment, use the Packages tab on the Python Environments window.
Note
For best results with conda environments, use conda 4.4.8 or later. Keep in mind that conda versions are different from Anaconda versions. You can install suitable versions of Miniconda (Visual Studio 2019 and Visual Studio 2022) and Anaconda (Visual Studio 2017) through the Visual Studio Installer.
To see the conda version, where conda environments are stored, and other information, run the
conda info
command at an Anaconda command prompt (a command prompt where Anaconda is in the path):
conda info
Your conda environment folders appear as follows:
envs directories : C:\Users\user\.conda\envs c:\anaconda3\envs C:\Users\user\AppData\Local\conda\conda\envs
Because conda environments aren’t stored with a project, they behave similarly to global environments. For example, installing a new package into a conda environment makes that package available to all projects that use the environment.
For Visual Studio 2017 version 15.6 and earlier, you can use conda environments by pointing to them manually as described in Manually identify an existing environment.
Visual Studio 2017 version 15.7 and later detects conda environments automatically and displays them in the Python Environments window as described in the next section.
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.
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.
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.
Phản hồi
Gửi và xem ý kiến phản hồi dành cho
Skip to main content
Skip to main content
Microsoft
Visual Studio
Visual Studio
Visual Studio
Home
Learn About Feedback
Guidelines
Problems
Suggestions
Code of Conduct
Downloads
Support
Visual Studio IDE
Visual Studio Code
Azure DevOps
Team Foundation Server
Accounts and Subscriptions
Subscriber Access
More
All Microsoft
Global
Microsoft Security
Azure
Dynamics 365
Microsoft 365
Microsoft Teams
Windows 365
Tech & innovation
Tech & innovation
Microsoft Cloud
AI
Azure Space
Mixed reality
Microsoft HoloLens
Microsoft Viva
Quantum computing
Sustainability
Industries
Industries
Education
Automotive
Financial services
Government
Healthcare
Manufacturing
Retail
All industries
Partners
Partners
Find a partner
Become a partner
Partner Network
Find an advertising partner
Become an advertising partner
Azure Marketplace
AppSource
Resources
Resources
Blog
Microsoft Advertising
Developer Center
Documentation
Events
Licensing
Microsoft Learn
Microsoft Research
View Sitemap
Sign in
You need to enable JavaScript to run this app.
Sorry this browser is no longer supported
Please use any other modern browser like ‘Microsoft Edge’.
Create and manage Python environments in Visual Studio
A Python environment is a context in which you run Python code and includes global, virtual, and conda environments. An environment consists of an interpreter, a library (typically the Python Standard Library), and a set of installed packages. These components together determine valid language constructs and syntax, operating-system functionality that you can access, and packages you can use.
In Visual Studio on Windows, you use the Python Environments window, as described in this article, to manage environments and select one as the default for new projects. Other aspects of environments are found in the following articles:
-
For any given project, you can select a specific environment rather than use the default.
-
For details on creating and using virtual environments for Python projects, see Use virtual environments.
-
If you want to install packages in an environment, refer to the Packages tab reference.
-
To install another Python interpreter, see Install Python interpreters. In general, if you download and run an installer for a mainline Python distribution, Visual Studio detects that new installation and the environment appears in the Python Environments window and can be selected for projects.
Note
You can manage environments for Python code that open as a folder by selecting File > Open > Folder. The Python toolbar allows you to switch between all detected environments, and also add a new environment. The environment information is stored in the PythonSettings.json file in the Workspace .vs folder.
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.
Fix or delete invalid environments
If Visual Studio finds registry entries for an environment, but the path to the interpreter is invalid, then the Python Environments window shows the environment name in a strikeout font format as shown in the following image:
To correct an environment that you want to keep, first try using the environment installer’s Repair process. Most installers include a repair option.
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. |
Manually identify an existing environment
Use the following steps to identify an environment installed in a nonstandard location.
-
In the Python Environments window (or from the Python toolbar), select Add Environment to open the Add environment dialog.
-
In the Add environment dialog, on the Existing environment tab, set the Environment field to
After you select the
value, more fields are added to the dialog.
-
Set the Prefix path field to the path of the interpreter. You can set the field by browsing (…) to the path location.
-
After you select the path, the remaining fields are populated. Review the values and modify as needed. When you’re ready, select Add.
You can also review and modify details of the environment at any time in the Python Environments window.
-
In the Python Environments window, select the environment, and then select the Configure tab.
-
After you make changes, select the Apply command.
You can also remove the environment by using the Remove command. For more information, see Configure tab. This command isn’t available for autodetected environments.
Phản hồi
Gửi và xem ý kiến phản hồi dành cho
Trong bài viết kỳ này, Học Viện Agile sẽ hướng dẫn bạn cách download, cài đặt lập trình Python trên Visual Studio Code 2017 và các extension cần thiết để lập trình dễ dàng, tiện lợi hơn.
Visual Studio Code là trình hỗ trợ soạn thảo code có sẵn cho macOS, Windows và Linux. Visual Studio Code đi kèm với hệ sinh thái mở rộng phong phú, kèm các ngôn ngữ C++, C#, Java, Python, Go… Trình biên tập mã Visual Studio Code là phần mềm mã nguồn mở được phát triển bởi Microsoft, có khả năng hỗ trợ debug, đi kèm với Git, syntax highlighting, hoàn thành mã thông minh và cải tiến mã nguồn. Người dùng cũng có thể tùy chỉnh thay đổi theme, phím tắt và nhiều tùy chọn khác.
Visual Studio Code – công cụ hỗ trợ lập trình Python mạnh mẽ
Yêu cầu cấu hình tối thiểu
Ưu điểm
Nhược điểm
Để kiểm tra máy đã cài phiên bản Python 3 chưa, bạn sử dụng lệnh Python – version. Nếu máy đã cài đặt Python, màn hình sẽ hiện ra version và không báo lỗi.
Trong trường hợp máy chưa cài đặt Python, bạn download tại đây.
Đối với hệ điều hành macOS, sau khi cài đặt Python, các bạn vào thư mục Python tìm file Install Certificates.command. Nhấn Enter mở file, file tự động chạy và tạo certificates dùng khi chạy chương trình sau này.
Lưu ý: Nhớ đánh dấu chọn Add Python to PATH để câu lệnh có thể được chạy từ bất kỳ đâu.
Link tải bộ cài đặt Visual Studio Code
Nếu bạn đang phát triển các phần mềm liên quan đến data science, có thể cài đặt thêm các ứng dụng hỗ trợ tính toán và data science. Tham khảo thêm tại đây.
Ở cột bên phải, ấn tick vào các tùy chọn. Có thể bỏ qua phần này nếu bạn muốn sử dụng tùy chọn mặc định.
Diễn giải các tùy chọn
Tùy chọn | Giải thích |
Python distributions | Gồm các tùy chọn 32-bit, 64-bit và Python 2, Python 3, Miniconda, Anaconda2, Anaconda3. Anaconda là platform mã nguồn mở bao gồm nhiều gói cài đặt. |
Cookiecutter template support | Cài đặt Cookiecutte GUI để thêm template, tạo project và file. Xem thêm tại đây. |
Python web support | Cài đặt công cụ phát triển web như HTML, CSS và JavaScript cùng Bottle, Flask, Django. Xem thêm tại đây. |
Python native development tools | Cài đặt trình biên dịch C++ và các ứng dụng cần thiết khác để phát triển thêm các phần mở rộng cho Python. |
Sau khi tải về trình cài đặt, các bạn có thể chọn Modify (điều chỉnh), Launch, Repair hoặc Uninstall Visual Studio. Nút Modify cho phép bạn tùy chọn cập nhật khi có bản updates của Visual Studio.
Visual Studio IntelliCode là extension giúp hỗ trợ viết code tốt hơn nhờ trí tuệ nhân tạo. Trong tương lai, extension này có thể sẽ được tích hợp ngay trong Visual Studio Code.
Visual Studio Intellicode hỗ trợ viết code nhanh, đơn giản
Để tiết kiệm thời gian chỉnh sửa cài đặt trình soạn thảo code, bạn có thể sử dụng Setting Sync. Extension này dùng GitHub Gist để đồng bộ cài đặt, người dùng chỉ cần thay đổi trên một máy là có thể dễ dàng đồng bộ trên nhiều máy khác. Quá trình đồng bộ thường mất vài phút và có thể kiểm tra thiết lập thông qua mục tổng quan Extensions trong Visual Studio Code.
Extension phù hợp cho các lập trình viên cần chỉnh sửa file cấu hình hệ thống, cho phép hoàn thành kiểu Intellisense cho tên file, nhập đường dẫn dài mà không cần lưu vào bộ nhớ.
GitLens có chế độ xem khác biệt, giúp người dùng so sánh các commit và branch, cho phép người sử dụng tìm kiếm thông tin lịch sử commit dự án, tên tác giả, file…
GitLens giúp lập trình viên xem khác biệt giữa commit và branch
Code Time giúp theo dõi hoạt động của lập trình viên Visual Studio Code, báo cáo cho bạn khoảng thời gian nào là tốt nhất trong ngày để lập trình. Ngoài ra, người dùng có thể thiết lập chế độ gửi báo cáo qua email hàng tuần hoặc lên lịch cùng Google Calendar.
Theo dõi thời gian hoạt động với Code Time để tăng năng suất và hiệu quả làm việc
Trên đây là một số hướng dẫn giúp các bạn dễ dàng cài đặt và lập trình Python trên Visual Studio 2017. Ngoài kiến thức chuyên môn, để tiến xa hơn trong sự nghiệp, lập trình viên cần trang bị thêm kỹ năng quản trị dự án, điều phối công việc để phát huy tối đa năng lực của đội nhóm. Các bạn có thể tham khảo thêm về Agile Software Development – tư duy phát triển phần mềm đem lại hiệu quả và năng suất cao, đang là sự lựa chọn hàng đầu của khách hàng & nhà phát triển. Mô hình Agile đã được áp dụng rất rộng rãi trên thế giới và bắt được phát triển mạnh mẽ tại Việt Nam. Học viện Agile tự hào là đơn vị đầu tiên đưa Agile đến gần hơn với các doanh nghiệp và cá nhân trong ngành công nghệ tại Việt Nam.
Chưa có sản phẩm nào trong giỏ hàng.
Install Python support in Visual Studio
Python support is available only on Visual Studio for Windows. On Mac and Linux, Python support is available through Visual Studio Code.
Keywords searched by users: anaconda visual studio 2017
Categories: Chia sẻ 39 Anaconda Visual Studio 2017
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/