python中打开文件的代码_快速入门 — 打开 Python 代码文件夹 - Visual Studio | Microsoft Docs...

快速入门:打开并运行文件夹中的 Python 代码Quickstart: Open and run Python code in a folder

03/12/2019

JoshuaPartlow.png?size=32

olprod.png?size=32

本文内容

在 Visual Studio 2019 中安装 Python 支持后,就可以在 Visual Studio 2019 中轻松地运行现有 Python 代码,而无需创建 Visual Studio 项目。Once you've installed Python support in Visual Studio 2019, it's easy to run existing Python code in Visual Studio 2019 without creating a Visual Studio project.

备注

在 Visual Studio 2017 及更早版本中,需要创建 Visual Studio 项目才能运行 Python 代码,使用内置项目模板可以轻松执行此操作。Visual Studio 2017 and earlier require you to create a Visual Studio project to run Python code, which you can easily do using a built-in project template.

在本演练中,可以将任何文件夹与你喜欢的 Python 代码搭配使用。For this walkthrough, you can use any folder with Python code that you like. 若要按照如下所示的示例操作,请在相应文件夹中使用命令 git clone https://github.com/gregmalcolm/python_koans 将 gregmalcolm/python_koans GitHub 存储库克隆到你的计算机。To follow along with the example shown here, clone the gregmalcolm/python_koans GitHub repository to your computer using the command git clone https://github.com/gregmalcolm/python_koans in an appropriate folder.

在“启动”窗口中启动 Visual Studio 2019,然后在“开始”栏底部选择“打开” 。Launch Visual Studio 2019 and in the start window, select Open at the bottom of the Get started column. 或者,如果已在运行 Visual Studio,请改为选择“文件” > “打开” > “文件夹” 命令。Alternately, if you already have Visual Studio running, select the File > Open > Folder command instead.

01-open-local-folder.png?view=vs-2019

导航到包含 Python 代码的文件夹,然后选择“选择文件夹” 。Navigate to the folder containing your Python code, then choose Select Folder. 如果使用的是 python_koans 代码,请务必选中克隆文件夹中的 python3 文件夹。If you're using the python_koans code, make sure to select the python3 folder within the clone folder.

02-select-folder.png?view=vs-2019

Visual Studio 将在解决方案资源管理器中的“文件夹视图”中显示该文件夹 。Visual Studio displays the folder in Solution Explorer in what's called Folder View. 可以使用文件夹名称左边缘的箭头展开和折叠文件夹:You can expand and collapse folders using the arrows on the left edges of the folder names:

03-expand-collapse-folders.png?view=vs-2019

打开 Python 文件夹时,Visual Studio 将创建几个隐藏文件夹来管理与项目相关的设置。When opening a Python folder, Visual Studio creates several hidden folders to manage settings related to the project. 若要查看这些文件夹(以及任何其他隐藏的文件和文件夹,如 .git 文件夹),请选择 “显示所有文件”工具栏按钮:To see these folders (and any other hidden files and folders, such as the .git folder), select the Show All Files toolbar button:

05-view-hidden-folders.png?view=vs-2019

若要运行代码,首先需要确定启动文件或主程序文件。To run the code, you first need to identify the startup or primary program file. 在此处显示的示例中,启动文件为 contemplate-koans.py 。In the example shown here, the startup file contemplate-koans.py. 右键单击该文件,然后选择“设为启动项” 。Right-click that file and select Set as Startup Item.

06-set-as-startup-item-command.png?view=vs-2019

重要

如果启动项不在已打开的文件夹的根目录中,还必须将一行添加到启动配置 JSON 文件中,如设置工作目录部分中所述。If your startup item is not located in the root of the folder you opened, you must also add a line to the launch configuration JSON file as described in the section, Set a working directory.

按 Ctrl +F5 ,或依次选择“调试” > “启动但不调试” 运行代码 。Run the code by pressing Ctrl+F5 or selecting Debug > Start without Debugging. 另外,还可以选择显示带有播放按钮的启动项的工具栏按钮,在 Visual Studio 调试程序中运行代码。You can also select the toolbar button that shows the startup item with a play button, which runs code in the Visual Studio debugger. 在所有情况下,Visual Studio 会检测到启动项是一个 Python 文件,因此会在默认 Python 环境中自动运行代码。In all cases, Visual Studio detects that your startup item is a Python file, so it automatically runs the code in the default Python environment. (该环境显示在工具栏上启动项的右侧。)(That environment is shown to the right of the startup item on the toolbar.)

07-start-debug-toolbar.png?view=vs-2019

程序的输出将显示在单独的命令窗口中:The program's output appears in a separate command window:

08-result-window.png?view=vs-2019

若要在其他环境中运行代码,请从工具栏上的下拉列表框控件中选择该环境,然后再次启动启动项。To run the code in a different environment, select that environment from the drop-down control on the toolbar, then launch the startup item again.

若要关闭 Visual Studio 中的文件夹,请选择“文件” > “关闭文件夹” 菜单命令。To close the folder in Visual Studio, select the File > Close folder menu command.

设置工作目录Set a working directory

默认情况下,Visual Studio 会运行作为该同一文件夹的根目录中的文件夹打开的 Python 项目。By default, Visual Studio runs a Python project opened as a folder in the root of that same folder. 但是,项目中的代码可假定正在子文件夹中运行 Python。The code in your project, however, might assume that Python is being run in a subfolder. 例如,假定打开 python_koans 存储库的根文件夹,然后将 python3/contemplate-koans.py 文件设置为启动项。For example, suppose you open the root folder of the python_koans repository and then set the python3/contemplate-koans.py file as startup item. 如果随后运行代码,会看到错误,指出找不到 koans.txt 文件 。If you then run the code, you see an error that the koans.txt file cannot be found. 发生此错误是因为 contemplate-koans.py 假定正在 python3 文件夹(而不是存储库根目录)中运行 Python。This error happens because contemplate-koans.py assumes that Python is being run in the python3 folder rather than the repository root.

在这种情况下,还必须将一行添加到启动配置 JSON 文件以指定工作目录:In such cases, you must also add a line to the launch configuration JSON file to specify the working directory:

在解决方案资源管理器中右键单击 Python (.py ) 启动文件 ,然后选择“调试和启动设置” 。Right-click the Python (.py) startup file in Solution Explorer and select Debug and Launch Settings.

09-debug-launch-settings-menu-command.png?view=vs-2019

在出现的“选择调试程序” 对话框中,选择“默认” ,然后选择“选择” 。In the Select debugger dialog box that appears, select Default and then choose Select.

10-select-debugger.png?view=vs-2019

备注

如果没有看到“默认”选项,请确保在选择“调试和启动设置”命令时选择了 Python .py 文件。If you don't see Default as a choice, be sure that you chose a Python .py file when selecting the Debug and Launch Settings command. Visual Studio 利用文件类型来确定要显示的调试程序选项。Visual Studio uses the file type to determine which debugger options to display.

Visual Studio 会打开名为 launch.vs.json 的文件 ,该文件位于隐藏的 .vs 文件夹中。Visual Studio opens a file named launch.vs.json, which is located in the hidden .vs folder. 此文件描述项目的调试上下文。This file describes the debugging context for the project. 若要指定工作目录,请为 "workingDirectory" 添加一个值,如 python-koans 示例的 "workingDirectory": "python3" 中所示:To specify a working directory, add a value for "workingDirectory", as in "workingDirectory": "python3" for python-koans example:

{

"version": "0.2.1",

"defaults": {},

"configurations": [

{

"type": "python",

"interpreter": "(default)",

"interpreterArguments": "",

"scriptArguments": "",

"env": {},

"nativeDebug": false,

"webBrowserUrl": "",

"project": "python3\\contemplate_koans.py",

"name": "contemplate_koans.py",

"workingDirectory": "python3"

}

]

}

保存该文件并再次启动程序,现在它在指定文件夹中运行。Save the file and launch the program again, which now runs in the specified folder.

后续步骤Next steps

请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值