将外部 Python IDE 与 Power BI 一起使用Use an external Python IDE with Power BI

借助 Power BI Desktop,可以使用外部的 Python IDE(集成开发环境)创建并优化 Python 脚本,然后将这些脚本用于 Power BI 中。With Power BI Desktop, you can use your external Python IDE (Integrated Development Environment) to create and refine Python scripts, then use those scripts in Power BI.

“选项”对话框的屏幕截图,其中“检测到的 Python IDE”字段中已输入 Visual Studio Code。

启用外部 Python IDEEnable an external Python IDE

可以从 Power BI Desktop 启动外部 Python IDE,然后自动导入数据并显示在 Python IDE 中。You can launch your external Python IDE from Power BI Desktop and have your data automatically imported and displayed in the Python IDE. 之后,可以修改此外部 Python IDE 中的脚本,然后将其重新粘贴至 Power BI Desktop 中来创建 Power BI 视觉对象和报表。From there, you can modify the script in that external Python IDE, then paste it back into Power BI Desktop to create Power BI visuals and reports.

可以指定要使用哪个 Python IDE,并从 Power BI Desktop 自动启动该 Python IDE。You can specify which Python IDE you would like to use, and have it launch automatically from within Power BI Desktop.

要求Requirements

要使用此功能,需要在本地计算机上安装 Python IDE。To use this feature, you need to install a Python IDE on your local computer. Power BI Desktop 不包含 Python 引擎,也不部署和安装该引擎,因此必须在本地计算机上单独安装 Python。Power BI Desktop does not include, deploy, or install the Python engine, so you must separately install Python on your local computer. 通过以下选项,你可以选择使用哪个 Python IDE:You can choose which Python IDE to use, with the following options:

  • 你可以安装最喜欢的 Python IDE,其中大部分都是免费的,例如 Visual Studio Code 下载页You can install your favorite Python IDE, many of which are available for free, such as the Visual Studio Code download page.

  • Power BI Desktop 还支持 Visual Studio。Power BI Desktop also supports Visual Studio.

  • 此外,还可以安装不同的 Python IDE,并通过执行以下任一操作,使 Power BI Desktop 启动相应 Python IDE:You can also install a different Python IDE and have Power BI Desktop launch that Python IDE by doing one of the following:

    • 可以将 .PY 文件与 Power BI Desktop 要启动的外部 IDE 相关联。You can associate .PY files with the external IDE you want Power BI Desktop to launch.

    • 通过从“选项”对话框的“Python 脚本选项”部分中选择“其他”,你可以指定 Power BI Desktop 应启动的 .exe。You can specify the .exe that Power BI Desktop should launch by selecting Other from the Python Script Options section of the Options dialog. 通过转到 “文件”>“选项和设置”>“选项” ,你可以打开“选项”对话框。You can bring up the Options dialog by going to File > Options and settings > Options.

      “选项”对话框的屏幕截图,其中“检测到的 Python IDE”字段中已输入“其他”。

如果安装了多个 Python IDE,则通过在“选项”对话框的“检测到的 Python IDE”下拉列表中进行选择就可以指定要启动哪个 Python IDE。If you have multiple Python IDEs installed, you can specify which will be launched by selecting it from the Detected Python IDEs drop-down in the Options dialog.

默认情况下,Power BI Desktop 将启动 Visual Studio Code 作为外部 Python IDE(如果它已安装在本地计算机上);如果未安装 Visual Studio Code,而是安装了 Visual Studio,则将启动 Visual Studio。By default, Power BI Desktop will launch Visual Studio Code as the external Python IDE if it's installed on your local computer; if Visual Studio Code is not installed and you have Visual Studio, that will be launched instead. 如果这些 Python IDE 均未安装,则将启动与 .PY 文件关联的应用程序。If neither of those Python IDEs is installed, the application associated with .PY files is launched.

如果 .PY 文件不存在任何关联,则可以在“选项”对话框的“浏览到你的首选 Python IDE”部分中指定自定义 IDE 的路径。And if no .PY file association exists, it's possible to specify a path to a custom IDE in the Browse to your preferred Python IDE section of the Options dialog. 通过选择 Power BI Desktop 中“启动 Python IDE”箭头图标旁边的“设置”齿轮图标,你还可以启动不同的 Python IDE。You can also launch a different Python IDE by selecting the Settings gear icon beside the Launch Python IDE arrow icon, in Power BI Desktop.

通过 Power BI Desktop 启动 Python IDELaunch a Python IDE from Power BI Desktop

要通过 Power BI Desktop 启动 Python IDE,请执行以下步骤:To launch a Python IDE from Power BI Desktop, take the following steps:

  1. 将数据加载至 Power BI DesktopLoad data into Power BI Desktop.

  2. 从“字段”窗格选择要使用的字段。Select some fields from the Fields pane that you want to work with. 如果尚未启用脚本视觉对象,则会提示你完成此操作。If you haven't enabled script visuals yet, you'll be prompted to do so.

    “启用脚本视觉对象”对话框的屏幕截图,提示你启用它。

  3. 启用脚本视觉对象后,可以从“可视化效果”窗格中选择 Python 视觉对象,此操作将创建空白的 Python 视觉对象来显示脚本结果。When script visuals are enabled, you can select a Python visual from the Visualizations pane, which creates a blank Python visual that's ready to display the results of your script. 同时也会显示“Python 脚本编辑器”窗格。The Python script editor pane also appears.

    “可视化效果”窗格的屏幕截图,其中显示了一个空白的 Python 视觉对象。

  4. 现在你可以选择要用于 Python 脚本的字段。Now you can select the fields you want to use in your Python script. 选择字段后,“Python 脚本编辑器”字段会基于所选的一个或多个字段自动创建脚本代码。When you select a field, the Python script editor field automatically creates script code based on the field or fields you select. 可在“Python 脚本编辑器”窗格中直接创建(或粘贴)Python 脚本,也可将其留空。You can either create (or paste) your Python script directly in the Python script editor pane, or you can leave it empty.

    “可视化效果”窗格的屏幕截图,其中显示了一个空白 的 Python 视觉,脚本编辑器中带有脚本代码。

    备注

    Python 视觉对象的默认聚合类型是“不汇总”。The default aggregation type for Python visuals is do not summarize.

  5. 现在可以直接从 Power BI Desktop 中启动 Python IDE。You can now launch your Python IDE directly from Power BI Desktop. 如下图所示,从“Python 脚本编辑器”标题栏的右侧找到并选中“启动 Python IDE”按钮。Select the Launch Python IDE button, found on the right side of the Python script editor title bar, as shown below.

    “Python 脚本编辑器”的屏幕截图,其中显示了如何启动 Python IDE。

  6. 如下图所示,Power BI Desktop 将启动特定的 Python IDE(在该图中,Visual Studio Code 是默认 Python IDE)。Your specified Python IDE is launched by Power BI Desktop, as shown in the following image (in this image, Visual Studio Code is the default Python IDE).

    在 Visual Studio Code 中显示的 Python IDE 的屏幕截图。

    备注

    Power BI Desktop 会添加脚本的前三行,这样一旦运行该脚本,就可以从 Power BI Desktop 中导入数据。Power BI Desktop adds the first three lines of the script so it can import your data from Power BI Desktop once you run the script.

  7. 在 Power BI Desktop 的“Python 脚本编辑器”窗格中创建的任何脚本都会从 Python IDE 的第 4 行开始显示。Any script you created in the Python script editor pane of Power BI Desktop appears starting in line 4 in your Python IDE. 此时,可在 Python IDE 中自行创建 Python 脚本。At this point, you can create your Python script in the Python IDE. 在 Python IDE 中完成 Python 脚本后,必须将其复制并重新粘贴至 Power BI Desktop 的“Python 脚本编辑器”窗格中,注意不包括 Power BI Desktop 自动生成的前三行脚本。Once your Python script is complete in your Python IDE, you need to copy and paste it back into the Python script editor pane in Power BI Desktop, excluding the first three lines of the script that Power BI Desktop automatically generated. 请勿将脚本的前三行复制到 Power BI Desktop,这三行仅用于将数据从 Power BI Desktop 导入到 Python IDE。Do not copy the first three lines of script back into Power BI Desktop, those lines were only used to import your data to your Python IDE from Power BI Desktop.

已知限制Known limitations

直接从 Power BI Desktop 中启动 Python IDE 具有部分限制:Launching a Python IDE directly from Power BI Desktop has a few limitations:

  • 不支持自动将脚本从 Python IDE 中导出到 Power BI Desktop。Automatically exporting your script from your Python IDE into Power BI Desktop is not supported.

后续步骤Next steps

查看以下更多信息,了解有关 Power BI 中的 Python。Take a look at the following additional information about Python in Power BI.