Installing and Setting Up Python for Power BI

Leave a comment

Python can be used in Power BI in several ways. These include:

  • Creating visualizations using Python
  • Using Python as a data source
  • Scripting in Python to prepare data

We will go through examples of each of these in the following posts.

In this post, we will look at how to install Python in Power BI. We will then move onto how to use Python in various scenarios in Power BI.

To install Python for Power BI, first go to the Python download page at https://www.python.org/ and click on Downloads:

Click Download Python:

This will download the file:

Check Add Python to PATH and click to Install. Note this also installs IDLE (the IDE), pip (the package installer) and documentation:

Click Close:

Note the links above are:

Now, IDLE is installed, and we can access it through the Windows Start menu:

Opening it, we can start typing commands:

Let’s see which modules are installed, by typing:

help("modules")

We see the modules installed:

Power BI requires 2 modules to be installed, pandas and matplotlib, which we can see are not installed. Let’s install them through the Windows command prompt.

First, open a Windows Command prompt and type python. We can see python is installed and the version:

Exit python by typing exit() to return back to the Windows command prompt:

To install the modules, we can use pip, as follows:

pip install PackageName

To install pandas, we would use:

pip install pandas

Ensure to run this in the command prompt outside of Python:

We see pip can be upgraded, with the command:

python -m pip install --upgrade pip

And let’s run pip install matplotlib:

Our python environment is now ready for use. Let’s look at IDEs.

We will use Visual Studio Code as our IDE to write Python scripts. Note we can write scripts directly within Power BI as well, but configuring an IDE will help us when we write and debug code. Open VS Code and go to Extensions, and find the Python extension:

Click Install:

Now let’s create a new Python script. We will use the same one as in the Microsoft sample documentation:

import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print (df)

Save this to a file with the extension .py, for example, Power BI Test.py.

Now, right-click anywhere in the document and select Run Python File in Terminal:

This prints the output in the terminal in Visual Studio Code:

Visual Studio Code is now ready to use. Let’s set up Power BI Desktop.

In Power BI Desktop, select Options:

Click on Python Scripting. We can see the Detected Python home directory has been picked up. We can also see the Detected Python IDE has not. Change it to Other so we can select which IDE to use:

We will browse to the Visual Studio Code installation by clicking Browse:

We’re now ready to start using Python. In the next post, we will show how to use Python as a data source in Power BI.

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *