如何在 Windows 上的 Visual Studio 中安装 Python 支持How to install Python support in Visual Studio on Windows

若要安装针对 Visual Studio 的 Python 支持(亦称为“针对 Visual Studio 的 Python 工具 (PTVS)”),请按照 Visual Studio 版本对应部分中的说明操作:To install Python support for Visual Studio (also known as Python Tools for Visual Studio or PTVS), follow the instructions in the section that matches your version of Visual Studio:

若要在执行安装步骤后快速测试 Python 支持,请按 Alt+I 并输入 2+2 打开 Python 交互式窗口 。To quickly test Python support after following the installation steps, open the Python Interactive window by pressing Alt+I and entering 2+2. 如果看不到输出 4,请重新检查步骤。If you don't see the output of 4, recheck your steps.

提示

Python 工作负载包括有用的 Cookiecutter 扩展,扩展提供图形用户界面以发现模板、输入模板选项及创建项目和文件。The Python workload includes the helpful Cookiecutter extension that provides a graphical user interface to discover templates, input template options, and create projects and files. 有关详细信息,请参阅使用 CookiecutterFor details, see Use Cookiecutter.

备注

目前尚未在 Visual Studio for Mac 中提供 Python 支持,但可在 Mac 和 Linux 上通过 Visual Studio Code 获取相应支持。Python support is not presently available in Visual Studio for Mac, but is available on Mac and Linux through Visual Studio Code. 请参阅问题和解答See questions and answers.

Visual Studio 2019 和 Visual Studio 2017Visual Studio 2019 and Visual Studio 2017

  1. 下载并运行最新 Visual Studio 安装程序。Download and run the latest Visual Studio installer. 如果已安装 Visual Studio,则运行 Visual Studio 安装程序,选择“修改”选项(请参阅修改 Visual Studio),并转到步骤 2。If you have Visual Studio installed already, run the Visual Studio Installer, select the Modify option (see Modify Visual Studio) and go to step 2.

    提示

    Community Edition 适用于个体开发者、课堂学习、学术研究和开放源代码开发。The Community edition is for individual developers, classroom learning, academic research, and open source development. 对于其他用途,请安装 Visual Studio 2019 ProfessionalVisual Studio 2019 EnterpriseFor other uses, install Visual Studio 2019 Professional or Visual Studio 2019 Enterprise.

  2. 安装程序提供工作负载列表,即一组用于特定开发领域的相关选项。The installer presents you with a list of workloads, which are groups of related options for specific development areas. 对于 Python,请选择 Python 开发工作负载。For Python, select the Python development workload.

    Visual Studio 安装程序中的 Python 开发工作负载

    可选:如果使用数据科学,还可考虑使用数据科学和分析应用程序工作负载。Optional: if you're working with data science, also consider the Data science and analytical applications workload. 此工作负载包含 Python、R 和 F# 语言支持。This workload includes support for the Python, R, and F# languages. 有关详细信息,请参阅数据科学和分析应用程序工作负载For more information, see Data science and analytical applications workload.

    备注

    Python 和数据科学工作负载仅可用于 Visual Studio 2017 版本 15.2 及更高版本。The Python and Data Science workloads are available only with Visual Studio 2017 version 15.2 and later.

    可选:如果使用数据科学,还可考虑使用数据科学和分析应用程序工作负载。Optional: if you're working with data science, also consider the Data science and analytical applications workload. 此工作负载包含对 Python 和 F# 语言的支持。This workload includes support for the Python and F# languages. 有关详细信息,请参阅数据科学和分析应用程序工作负载For more information, see Data science and analytical applications workload.

  3. 如果需要,选择安装程序右侧的其他选项。On the right side of the installer, chose additional options if desired. 跳过此步骤,接受默认选项。Skip this step to accept the default options.

    Visual Studio 安装程序中的 Python 开发选项

    Visual Studio 2019 安装程序中的 Python 开发选项

    选项Option 描述Description
    Python 分发版本Python distributions 选择计划使用的可用选项任意组合,例如 Python 2、Python 3、Miniconda、Anaconda2 和 Anaconda3 分发版本的 32 位和 64 位变体。Choose any combination of the available options, such as 32-bit and 64-bit variants of the Python 2, Python 3, Miniconda, Anaconda2, and Anaconda3 distributions that you plan to work with. 每个组合都包含分发版本的解释器、运行时和库。Each includes the distribution's interpreter, runtime, and libraries. 具体来说,Anaconda 是开放数据科学平台,包含各种预安装的包。Anaconda, specifically, is an open data science platform that includes a wide range of pre-installed packages. (可随时返回 Visual Studio 安装程序来添加或删除分发版本。)说明:如果已安装 Visual Studio 安装程序之外的分发版本,则无需检查此处的等效选项。(You can return to the Visual Studio installer at any time to add or remove distributions.) Note: If you've installed a distribution outside of the Visual Studio installer, there's no need to check the equivalent option here. Visual Studio 会自动检测现有的 Python 安装。Visual Studio automatically detects existing Python installations. 请参阅“Python 环境”窗口See The Python Environments window. 此外,若有比安装程序中所显示的版本更高的 Python 版本可用,可以单独安装较高版本,并且 Visual Studio 会检测到它。Also, if a newer version of Python is available than what's shown in the installer, you can install that version separately and Visual Studio will detect it.
    Cookiecutter 模板支持Cookiecutter template support 安装 Cookiecutter 图形用户界面,用于发现模板、输入模板选项以及创建项目和文件。Installs the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. 请参阅使用 Cookiecutter 扩展See Use the Cookiecutter extension.
    Python Web 支持Python web support 安装用于 Web 开发的工具(包括 HTML、CSS 和 JavaScript 编辑支持)以及用于使用 Bottle、Flask 和 Django 框架的项目的模板。Installs tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. 请参阅 Python Web 项目模板See Python web project templates.
    Python IoT 支持Python IoT support 支持使用 Python 开发 Windows IoT Core。Supports Windows IoT Core development using Python.
    Python 本机开发工具Python native development tools 安装 C++ 编译器和其他必要组件用于开发 Python 本机扩展。Installs the C++ compiler and other necessary components to develop native extensions for Python. 请参阅创建适用于 Python 的 C++ 扩展See Create a C++ extension for Python. 若要获取全面的 C++ 支持,还请安装“使用 C++ 的桌面开发”工作负载。Also install the Desktop development with C++ workload for full C++ support.
    Azure 云服务核心工具Azure Cloud Services core tools 提供在 Python 中开发 Azure 云服务的其他支持。Provides additional support for developer Azure Cloud Services in Python. 请参阅 Azure 云服务项目See Azure cloud service projects.
    选项Option 描述Description
    Python 分发版本Python distributions 选择计划使用的可用选项任意组合,例如 Python 2、Python 3、Miniconda、Anaconda2 和 Anaconda3 分发版本的 32 位和 64 位变体。Choose any combination of the available options, such as 32-bit and 64-bit variants of the Python 2, Python 3, Miniconda, Anaconda2, and Anaconda3 distributions that you plan to work with. 每个组合都包含分发版本的解释器、运行时和库。Each includes the distribution's interpreter, runtime, and libraries. 具体来说,Anaconda 是开放数据科学平台,包含各种预安装的包。Anaconda, specifically, is an open data science platform that includes a wide range of pre-installed packages. (可随时返回 Visual Studio 安装程序来添加或删除分发版本。)说明:如果已安装 Visual Studio 安装程序之外的分发版本,则无需检查此处的等效选项。(You can return to the Visual Studio installer at any time to add or remove distributions.) Note: If you've installed a distribution outside of the Visual Studio installer, there's no need to check the equivalent option here. Visual Studio 会自动检测现有的 Python 安装。Visual Studio automatically detects existing Python installations. 请参阅“Python 环境”窗口See The Python Environments window. 此外,若有比安装程序中所显示的版本更高的 Python 版本可用,可以单独安装较高版本,并且 Visual Studio 会检测到它。Also, if a newer version of Python is available than what's shown in the installer, you can install that version separately and Visual Studio will detect it.
    Cookiecutter 模板支持Cookiecutter template support 安装 Cookiecutter 图形用户界面,用于发现模板、输入模板选项以及创建项目和文件。Installs the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. 请参阅使用 Cookiecutter 扩展See Use the Cookiecutter extension.
    Python Web 支持Python web support 安装用于 Web 开发的工具(包括 HTML、CSS 和 JavaScript 编辑支持)以及用于使用 Bottle、Flask 和 Django 框架的项目的模板。Installs tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. 请参阅 Python Web 项目模板See Python web project templates.
    Python 本机开发工具Python native development tools 安装 C++ 编译器和其他必要组件用于开发 Python 本机扩展。Installs the C++ compiler and other necessary components to develop native extensions for Python. 请参阅创建适用于 Python 的 C++ 扩展See Create a C++ extension for Python. 若要获取全面的 C++ 支持,还请安装“使用 C++ 的桌面开发”工作负载。Also install the Desktop development with C++ workload for full C++ support.
    Azure 云服务核心工具Azure Cloud Services core tools 提供在 Python 中开发 Azure 云服务的其他支持。Provides additional support for developer Azure Cloud Services in Python. 请参阅 Azure 云服务项目See Azure cloud service projects.
  4. 安装后,安装程序会提供用于修改、启动、修复或卸载 Visual Studio 的选项。After installation, the installer provides options to modify, launch, repair, or uninstall Visual Studio. 当已安装的所有组件均可使用 Visual Studio 更新时,“修改”按钮将更改为“更新” 。The Modify button changes to Update when updates to Visual Studio are available for any installed components. (随后在下拉菜单中提供“修改”选项。)还可搜索“Visual Studio”,从 Windows “开始”菜单启动 Visual Studio 及安装程序。(The Modify option is then available on the drop-down menu.) You can also launch Visual Studio and the installer from the Windows Start menu by searching on "Visual Studio".

    通过安装程序启动、修改或卸载 Visual Studio

疑难解答Troubleshooting

如果在 Visual Studio 中安装或运行 Python 时遇到问题,请尝试以下操作:If you encounter problems installing or running Python in Visual Studio, try the following:

  • 确定使用 Python CLI(即在命令提示符处运行 python.exe)时是否会出现相同的问题。Determine whether the same error occurs using the Python CLI, that is, running python.exe from a command prompt.
  • 使用 Visual Studio 安装程序中的“修复”选项。Use the Repair option in the Visual Studio installer.
  • 在 Windows 中通过“设置” > “应用和功能”修复或重新安装 Python 。Repair or reinstall Python through Settings > Apps & features in Windows.

示例错误:未能启动交互式进程:System.ComponentModel.Win32Exception (0x80004005):Microsoft.PythonTools.Repl.PythonInteractiveEvaluator.d__43.MoveNext() 处未知错误 (0xc0000135)。Example error: Failed to start interactive process: System.ComponentModel.Win32Exception (0x80004005): Unknown error (0xc0000135) at Microsoft.PythonTools.Repl.PythonInteractiveEvaluator.d__43.MoveNext().

Visual Studio 2015Visual Studio 2015

  1. 通过“控制面板”>“程序和功能”,选择 Microsoft Visual Studio 2015,然后选择“更改”,从而运行 Visual Studio 安装程序。Run the Visual Studio installer through Control Panel > Programs and Features, selecting Microsoft Visual Studio 2015 and then Change.

  2. 在安装程序中,选择“修改”。In the installer, select Modify.

  3. 选择“编程语言” > “针对 Visual Studio 的 Python 工具”,然后选择“下一步”:Select Programming Languages > Python Tools for Visual Studio and then Next:

    Visual Studio 2015 安装程序中的 PTVS 选项

  4. Visual Studio 安装程序完成后,安装所选的 Python 解释器Once Visual Studio setup is complete, install a Python interpreter of your choice. Visual Studio 2015 仅支持 Python 3.5 及更早版本;更高版本生成类似“Python 3.6 版本不受支持”的消息。Visual Studio 2015 supports only Python 3.5 and earlier; later versions generate a message like Unsupported Python version 3.6). 如果你已安装解释器且 Visual Studio 不会自动检测它,请参阅手动标识现有环境If you already have an interpreter installed and Visual Studio doesn't detect it automatically, see Manually identify an existing environment.

Visual Studio 2013 及更早版本Visual Studio 2013 and earlier

  1. 为你的 Visual Studio 版本安装相应版本的针对 Visual Studio 的 Python 工具:Install the appropriate version of Python Tools for Visual Studio for your version of Visual Studio:

  2. 安装所选的 Python 解释器Install a Python interpreter of your choice. 如果你已安装解释器且 Visual Studio 不会自动检测它,请参阅手动标识现有环境If you already have an interpreter installed and Visual Studio doesn't detect it automatically, see Manually identify an existing environment.

安装位置Install locations

默认情况下,为计算机上的所有用户安装 Python 支持。By default, Python support is installed for all users on a computer.

对于 Visual Studio 2019 和 Visual Studio 2017,Python 工作负载安装在 %ProgramFiles(x86)%\Microsoft Visual Studio\<VS_version>\<VS_edition>Common7\IDE\Extensions\Microsoft\Python 中,其中 <VS_version> 为 2019 或 2017,<VS_edition> 为 Community、Professional 或 Enterprise。For Visual Studio 2019 and Visual Studio 2017, the Python workload is installed in %ProgramFiles(x86)%\Microsoft Visual Studio\<VS_version>\<VS_edition>Common7\IDE\Extensions\Microsoft\Python where <VS_version> is 2019 or 2017 and <VS_edition> is Community, Professional, or Enterprise.

对于 Visual Studio 2015 及更早版本,安装路径如下所示:For Visual Studio 2015 and earlier, installation paths are as follows:

  • 32 位:32-bit:
    • 路径:%Program Files(x86)%\Microsoft Visual Studio <VS_ver>\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver>Path: %Program Files(x86)%\Microsoft Visual Studio <VS_ver>\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver>
    • 路径的注册表位置:HKEY_LOCAL_MACHINE\Software\Microsoft\PythonTools\<VS_ver>\InstallDirRegistry location of path: HKEY_LOCAL_MACHINE\Software\Microsoft\PythonTools\<VS_ver>\InstallDir
  • 64 位:64-bit:
    • 路径:%Program Files%\Microsoft Visual Studio <VS_ver>\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver>Path: %Program Files%\Microsoft Visual Studio <VS_ver>\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver>
    • 路径的注册表位置:HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\PythonTools\<VS_ver>\InstallDirRegistry location of path: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\PythonTools\<VS_ver>\InstallDir

其中:where:

  • <VS_ver> 是:<VS_ver> is:
    • Visual Studio 2015 14.014.0 for Visual Studio 2015
    • Visual Studio 2013 12.012.0 for Visual Studio 2013
    • Visual Studio 2012 11.011.0 for Visual Studio 2012
    • Visual Studio 2010 10.010.0 for Visual Studio 2010
  • <PTVS_ver> 是版本号,例如 2.2.2、2.1.1、2.0、1.5、1.1 或 1.0。<PTVS_ver> is a version number, such as 2.2.2, 2.1.1, 2.0, 1.5, 1.1, or 1.0.

特定于用户的安装(1.5 及更早版本)User-specific installations (1.5 and earlier)

仅允许为当前用户安装针对 Visual Studio 的 Python 工具 1.5 版及更早版本,这种情况下的安装路径是 %LocalAppData%\Microsoft\VisualStudio\<VS_ver>\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver>,其中 <VS_ver> 和 <PTVS_ver> 与上文所述相同。Python Tools for Visual Studio 1.5 and earlier allowed installation for the current user only, in which case the installation path is %LocalAppData%\Microsoft\VisualStudio\<VS_ver>\Extensions\Microsoft\Python Tools for Visual Studio\<PTVS_ver> where <VS_ver> and <PTVS_ver> are the same as described above.