Python Script Runner

-->

To run Python script on a text editor like VS Code (Visual Studio Code) then you will have to do the following: Go in the extension section or press ‘ Ctrl+Shift+X ’ on windows, then search and install the extension named ‘ Python ’ and ‘ Code Runner ’. Restart your vs code after that. On Windows, the Python 3.4 interpreter is located at C:Python34python.exe.Alternatively, the convenient py.exe program will read the shebang line at the top of the.py file’s source code and run the appropriate version of Python for that script. The py.exe program will make sure to run the Python program with the correct version of Python if multiple versions are installed on your computer.

You can run Python scripts directly in Power BI Desktop and import the resulting datasets into a Power BI Desktop data model.

Install Python

To run Python scripts in Power BI Desktop, you need to install Python on your local machine. You can download Python from the Python website. The current Python scripting release supports Unicode characters and spaces in the installation path.

Install required Python packages

The Power BI Python integration requires the installation of two Python packages:

  • Pandas. A software library for data manipulation and analysis. It offers data structures and operations for manipulating numerical tables and time series. 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.
  • Matplotlib. A plotting library for Python and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits, such as Tkinter, wxPython, Qt, or GTK+.

In a console or shell, use the pip command-line tool to install the two packages. The pip tool is packaged with more recent Python versions.

Enable Python scripting

To enable Python scripting:

  1. In Power BI Desktop, select File > Options and settings > Options > Python scripting. The Python script options page appears.

  2. If necessary, specify your local Python installation path in Detected Python home directories.

    In the above image, the Python's installation local path is C:Python. Make sure the path is for the local Python installation you want Power BI Desktop to use.

  3. Select OK.

Once you specify your Python installation, you’re ready to begin running Python scripts in Power BI Desktop.

Run Python scripts

In just a few steps, you can run Python scripts and create a data model. From this model, you can create reports and share them on the Power BI service.

Prepare a Python script

First, create a script in your local Python development environment and make sure it runs successfully. For example, here's a simple Python script that imports pandas and uses a data frame:

When run, this script returns:

Script

When preparing and running a Python script in Power BI Desktop, there are a few limitations:

  • Only pandas data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame
  • Any Python script that runs longer than 30 minutes times out
  • Interactive calls in the Python script, such as waiting for user input, halts the script’s execution
  • 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

Run your Python script and import data

To run your Python Script in Power BI Desktop:

  1. In the Home ribbon, select Get data > Other.

  2. Select Other > Python script as shown in the following image:

  3. Select Connect. Your local latest installed Python version is selected as your Python engine. Copy your script into the Python script dialog box that appears. Here, we enter the simple Python script shown before.

  4. Select OK. If the script runs successfully, the Navigator appears and you can load the data and use it. For the example, select df, as shown in the image, then Load.

Troubleshooting

If Python isn't installed or identified, a warning displays. You can also see a warning if you have multiple local machine installations. Revisit and review the previous Install Python and Enable Python scripting sections.

Pycharm

Using custom Python distributions

Power BI executes scripts directly by using the python.exe executable from a user-provided directory (provided through the settings page). Distributions that require an extra step to prepare the environment (for example, Conda) might encounter an issue where their execution fails.

We recommend using the official Python distribution from https://www.python.org/ to avoid related issues.

As a possible solution, you can start Power BI Desktop from your custom Python environment prompt.

Python Script Download

Refresh

You can refresh a Python script in Power BI Desktop. To refresh, go to the Home ribbon and select Refresh. When you refresh a Python script, Power BI Desktop runs the Python script again.

Python

Known Limitations

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. For more information check out Using enhanced dataset metadata (preview).

Next steps

Python Script Example

Take a look at the following additional information about Python in Power BI.