在 Power BI Desktop 中运行 Python 脚本Run Python scripts in Power BI Desktop

06/02/2020

本文内容

你可以直接在 Power BI Desktop 中运行 Python 脚本,并将结果数据集导入 Power BI Desktop 数据模型。You can run Python scripts directly in Power BI Desktop and import the resulting datasets into a Power BI Desktop data model.

安装 PythonInstall Python

若要在 Power BI Desktop 中运行 Python 脚本,需要在本地计算机上安装 Python。To run Python scripts in Power BI Desktop, you need to install Python on your local machine. 可以从Python 网站下载 Python。You can download Python from the Python website. 当前的 Python 脚本版本支持在安装路径中包含 Unicode 字符和空格。The current Python scripting release supports Unicode characters and spaces in the installation path.

安装所需的 Python 包Install required Python packages

Power BI Python 集成需要安装两个 Python 包:The Power BI Python integration requires the installation of two Python packages:

用于数据操作和分析的软件库。A software library for data manipulation and analysis. 它提供了用于处理数值表和时序的数据结构和操作。It offers data structures and operations for manipulating numerical tables and time series. 导入的数据必须位于 pandas 数据帧中。Your imported data must be in a pandas data frame. 数据帧是一种二维数据结构。A data frame is a two-dimensional data structure. 例如,数据按表格的形式分布在行和列中。For example, data is aligned in a tabular fashion in rows and columns.

Python 的绘图库及其数值数学扩展 NumPy。A plotting library for Python and its numerical mathematics extension NumPy. 它提供了面向对象的 API,可使用通用 GUI 工具包(例如 Tkinter、wxPython、Qt 或 GTK+)将绘图嵌入到应用程序中。It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits, such as Tkinter, wxPython, Qt, or GTK+.

在控制台或 shell 中,使用 pip 命令行工具安装这两个包。In a console or shell, use the pip command-line tool to install the two packages. Pip 工具与最新的 Python 版本打包在一起。The pip tool is packaged with more recent Python versions.

pip install pandas

pip install matplotlib

启用 Python 脚本Enable Python scripting

要启用 Python 脚本,请执行以下操作:To enable Python scripting:

在 Power BI Desktop 中,依次选择“文件” > “选项和设置” > “选项” > “Python 脚本” 。In Power BI Desktop, select File > Options and settings > Options > Python scripting. 你将看到“Python 脚本选项”页面。The Python script options page appears.


如有必要,请在“检测到的 Python 主目录”中指定本地 Python 安装路径。If necessary, specify your local Python installation path in Detected Python home directories.

在上图中,Python 的安装本地路径为 C:\Python。In the above image, the Python's installation local path is C:\Python. 确保该路径用于希望 Power BI Desktop 使用的本地 Python 安装。Make sure the path is for the local Python installation you want Power BI Desktop to use.

选择“确定”。Select OK.

指定 Python 安装后,即可开始在 Power BI Desktop 中运行 Python 脚本。Once you specify your Python installation, you’re ready to begin running Python scripts in Power BI Desktop.

运行 Python 脚本Run Python scripts

只需几个步骤,即可运行 Python 脚本并创建数据模型。In just a few steps, you can run Python scripts and create a data model. 在本模型中,可以创建报表并在 Power BI 服务上共享它们。From this model, you can create reports and share them on the Power BI service.

准备 Python 脚本Prepare a Python script

首先,请在本地 Python 开发环境中创建脚本并确保其成功运行。First, create a script in your local Python development environment and make sure it runs successfully. 例如,下面是一个简单的 Python 脚本,用于导入 pandas 并使用数据帧:For example, here's a simple Python script that imports pandas and uses a data frame:

import pandas as pd

data = [['Alex',10],['Bob',12],['Clarke',13]]

df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)

print (df)

运行时,此脚本返回:When run, this script returns:

Name Age

0 Alex 10.0

1 Bob 12.0

2 Clarke 13.0

在 Power BI Desktop 中准备和运行 Python 脚本时,会有一些限制:When preparing and running a Python script in Power BI Desktop, there are a few limitations:

由于仅导入 Pandas 数据帧,因此请确保要导入到 Power BI 的数据都以数据帧表示Only pandas data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame

任何 Python 脚本若运行时间超过 30 分钟就会超时Any Python script that runs longer than 30 minutes times out

Python 脚本中的交互式调用(如等待用户输入)会终止脚本执行Interactive calls in the Python script, such as waiting for user input, halts the script’s execution

在 Python 脚本中设置工作目录时,必须定义工作目录的完整路径,而非相对路径When setting the working directory within the Python script, you must define a full path to the working directory, rather than a relative path

当前不支持嵌套表Nested tables are currently not supported

运行 Python 脚本并导入数据Run your Python script and import data

要在 Power BI Desktop 中刷新 Python 脚本,请执行以下操作:To run your Python Script in Power BI Desktop:

在“主页”功能区中,选择“获取数据” > “其他”。In the Home ribbon, select Get Data > Other.

选择“其他” > “Python 脚本” ,如下图所示:Select Other > Python script as shown in the following image:


选择“连接”。Select Connect. 选择本地最新安装的 Python 版本作为 Python 引擎。Your local latest installed Python version is selected as your Python engine. 将脚本复制到显示的“Python 脚本”对话框中。Copy your script into the Python script dialog box that appears. 在这里,我们输入之前显示的简单 Python 脚本。Here, we enter the simple Python script shown before.


选择“确定”。Select OK. 如果脚本成功运行,则会显示“导航器”,你可以加载数据并使用它。If the script runs successfully, the Navigator appears and you can load the data and use it. 对于本示例,如图所示,选择“df”,然后选择“加载”。For the example, select df, as shown in the image, then Load.


故障排除Troubleshooting

如果未安装或未标识 Python,系统将显示警告。If Python isn't installed or identified, a warning displays. 如果有多个本地计算机安装,系统也会显示一条警告。You can also see a warning if you have multiple local machine installations. 重新回顾并查看之前的“安装 Python 并启用 Python 脚本”部分。Revisit and review the previous Install Python and Enable Python scripting sections.


使用自定义 Python 分发Using custom Python distributions

Power BI 直接通过从用户提供的目录(通过设置页面提供)使用 python.exe 可执行文件来执行脚本。Power BI executes scripts directly by using the python.exe executable from a user-provided directory (provided through the settings page). 如果分发需要额外的步骤来准备环境(例如 Conda),则可能会遇到执行失败的问题。Distributions that require an extra step to prepare the environment (for example, Conda) might encounter an issue where their execution fails.

为避免相关问题,建议使用来自 https://www.python.org/ 的官方 Python 分发。We recommend using the official Python distribution from https://www.python.org/ to avoid related issues.

一个可能的解决方案是,你可从自定义 Python 环境提示处启动 Power BI Desktop。As a possible solution, you can start Power BI Desktop from your custom Python environment prompt.

刷新Refresh

你可以在 Power BI Desktop 中刷新 Python 脚本。You can refresh a Python script in Power BI Desktop. 若要刷新,请转到“主页”功能区,然后选择“刷新” 。To refresh, go to the Home ribbon and select Refresh. 刷新 Python 脚本时,Power BI Desktop 会再次运行 Python 脚本。When you refresh a Python script, Power BI Desktop runs the Python script again.

已知限制Known Limitations

目前,你无法在创建时启用了增强型元数据(预览版)功能的报表中使用 Python 脚本。Currently you won't be able to use Python scripts in reports created with Enhanced Metadata (Preview) feature enabled. 现有报表将继续发挥作用。Existing reports will continue to work.

后续步骤Next steps

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