在 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.

power python_在 Power BI Desktop 中运行 Python 脚本相关推荐

  1. cmd命令行怎样运行python_在CMD命令行中运行python脚本的方法

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  2. android运行python脚本,在android应用程序中运行python脚本

    我想得到远程安装软件的列表电脑.为了我想在我的android中使用python脚本申请.现在,我有一个python脚本,它正在远程获取已安装软件的列表电脑.但是,我不知道如何在android中支持它. ...

  3. flask执行python脚本_如何在Flask中运行python脚本

    我有一个Flask脚本,可以创建网站并动态打印一些数据.-打印的数据应来自另一个python脚本. 我目前面临的问题是,如果我将执行python脚本的行放在执行Flask应用程序的行之前,它将运行Py ...

  4. excel运行python_使用PyXLL在Excel中执行Python脚本

    PyXLL是一款能够使得在Excel中能够执行Python脚本的插件,不过它是使用Excel扩展库(XLL)的方式,这跟平常加载一些常用的宏方式是一样的. 使用pyXLL,首先你必须安装Python, ...

  5. cmd无法运行python_通过Java-%1在cmd中运行python文件不是有效的Win32应用程序 - java...

    我正在尝试使用Java执行python代码.我的代码: public class PyTest { public static void main(String[] args) { Runtime.g ...

  6. electron调用python_在Electron app中运行python脚本

    asar Whether to package the application's source code into an archive, using Electron's archive form ...

  7. idle运行python_如何从IDLE交互式shell运行python脚本?

    How do I run a python script from within the IDLE interactive shell? The following throws an error: ...

  8. 怎么在cmd中运行python脚本_cmd中运行python脚本智能使用流程

    (此时的ScaleMode=自动变Vbuser) 更有趣的是用来计算字串高.宽的TextHeight/TextWidth也变成以座标0-100的方式来 表现了 On Error Resume Next ...

  9. shell运行python文件_在python shell中运行python文件的实现

    在python shell中运行python文件的实现 最近在学习flask开发,写好程序后需要在python shell中运行测试功能.专门抽时间研究了下,总结以防止以后遗忘. 这是测试文件的结构, ...

最新文章

  1. 清华博士的逆袭之路:从收到延毕警告到顶刊发文,我蛰伏了4年
  2. JS中相等运算符 == 隐式转换
  3. QT开发(六十六)——登录对话框的验证机制
  4. linux mkfs 源码,mkfs工具怎么移植到嵌入式平台下面
  5. win8计算机usb无法识别usb设备,Win8.1无法识别USB设备原因分析及解决办法(适合Win8)...
  6. xlrd对excel单列处理
  7. officeopenxml excelpackage 需要安装excel嘛_使用ABAP操作Excel的几种方法
  8. 阿里巴巴的独立环境是如何实现的
  9. python kotlin_在Python,Java和Kotlin中标记参数和重载
  10. 关于电子书下载源、转换、阅读软件、个人图书馆的建立
  11. java源程序文件扩展名_JAVA源代码的扩展名为( )
  12. 计算机二级excel常见函数函数多表求和,excel sumif函数多条件求和 sumif的高级用法:跨多表条件求和...
  13. 企业ERP系统如何选择?
  14. 软件测试cmm等级划分,CMM的五个等级及关键过程域
  15. 以衍复为例,聊聊当下的沪深300指数增强
  16. 开发板和电脑网线连接,电脑网线连接 ubuntu18.04 nfs搭建
  17. Android调用相机预览黑屏app passed NULL surface解决
  18. python36.dll下载_python36.dll文件下载,金山毒霸dll修复工具帮您解决文件丢失导致“python36.dll找不到”的系统问题...
  19. 基于Hyperledger Fabric实现药品溯源
  20. 2022年大数据技能大赛国赛(模块C,D)

热门文章

  1. 【60岁老人年审】老来网app养老保险年审的好伙伴【本地年审】【异地年审】【不参加年审无法领取养老保险金】...
  2. Linux下添加DB2用户
  3. 【Spring实战】注入非Spring Bean对象
  4. [CLPR] 用于加速训练神经网络的二阶方法
  5. Git 学习笔记--3.EGit使用手册
  6. Hadoop 2.2.0安装和配置lzo
  7. ROSA 2012 Enterprise Linux Server 发布
  8. 求助 windows server2008密码策略修改
  9. 男生遇到漂亮MM就变傻
  10. 选择MPLS或SD-WAN用于组织网络部署的几大原因—Vecloud微云