Yes, you can run Python inside Power BI, and the shortest working path is: install Python, point Power BI at it, then use either a Python visual or a Python script data source. Python in Power BI lets you clean data with pandas, run scikit-learn models, or draw charts that the built-in visuals cannot produce, all without leaving the report. The catch is that Python scripts run locally on your machine, so anyone opening the report needs Python installed with the same packages, and the Power BI service cannot refresh Python scripts unless you use a personal gateway. This guide walks through the setup and the two main places Python appears.
What You'll Need
- Power BI Desktop for Windows (Python integration is not available in Power BI Desktop for Mac).
- A Python 3 installation from python.org. The Microsoft Store version of Python is not recommended because Power BI may not detect it.
- The packages you plan to use, installed into that same Python environment. pandas is the usual starting point; matplotlib for charts; scikit-learn for models.
- A Power BI report with at least one table or query you want to work with.
Step-by-Step
- Install Python from python.org using the standard installer. On the first installer screen, tick Add python.exe to PATH before clicking Install. This single checkbox prevents most detection problems later.
- Open a Command Prompt and confirm the install with
python --version. You should see a 3.x version number. If you see an error, the PATH entry did not take; reinstall and tick the box. - Install your packages from the same Command Prompt with
pip install pandas matplotlib(add others as needed). If you use a virtual environment, note its full path to the python.exe file, because Power BI needs to point at that exact interpreter. - In Power BI Desktop, open File > Options and settings > Options. Menu labels can differ slightly by version, so look for the Python-related entry under the Global section if the exact wording is not there.
- In the Python script settings, set the Detected Python home directories or browse to the folder containing python.exe. If Power BI detected your install automatically, confirm the path is the one you expect. Click OK.
- Restart Power BI Desktop so the new interpreter path is picked up.
- To use Python as a data source, click Get data, search for Python script, and paste a script that returns a DataFrame. For example,
import pandas as pdthendf = pd.DataFrame({'a':[1,2,3]}). Power BI turns the result into a table you can load or transform further. - To clean or extend an existing table, open Power Query Editor, select the query, and use Transform > Run Python script. Your script receives the current table as a pandas DataFrame called
dataset, and whatever you assign back todatasetbecomes the new table. This is the most common use of Python in Power BI. - To draw a chart, add the Python visual from the Visualizations pane, drag the fields you need into its Values well, then paste plotting code into the script editor. The fields arrive as a DataFrame called
dataset, and you must callplt.show()at the end or nothing renders. - Save the report. When you publish to the Power BI service, remember that Python visuals and script steps render only if the refresh or viewing environment has Python available; for scheduled refresh you need a personal gateway on a machine with Python and the packages installed.
Common Mistakes
- Skipping the PATH checkbox. Power BI then cannot find Python, and the Options page shows no detected directory.
- Installing packages into a different Python. If
pipand Power BI point at different interpreters, your script fails with ModuleNotFoundError even though the package is installed. - Forgetting
plt.show()in a Python visual. The visual stays blank with no error message. - Assuming the service behaves like Desktop. Python steps do not run in the Power BI service without a gateway, and Python visuals need Python on the viewing machine in many configurations.
- Using a script that prints instead of returning. In Power Query, only the
datasetDataFrame is passed back; print output is ignored.
Tips
- Keep scripts short and deterministic. Avoid anything that downloads data at refresh time, because it slows refresh and can fail silently.
- Name your output DataFrame
datasetin Power Query steps so the next step sees it. - Test the script in a plain Python session first. If it works there but not in Power BI, the problem is almost always the interpreter path or a missing package.
- For Python visuals, set a fixed figure size and avoid interactive elements; the visual is a static image.
- Document the package list in the report notes so anyone refreshing it knows what to install.
Troubleshooting
If Power BI does not detect Python, first confirm python --version works in Command Prompt. If it does not, reinstall Python with the PATH option ticked. If it does, browse manually to the folder containing python.exe in the Options dialog, then restart Power BI.
If a script fails with ModuleNotFoundError, run pip show pandas and check the Location line. That path should sit under the same Python folder you configured in Power BI. Installing with python -m pip install pandas avoids picking up the wrong pip.
If a Python visual is blank, check that the last line calls plt.show(), that the fields are dragged into the Values well (not just selected), and that the script does not rely on a package that is missing. A red error bar in the visual usually names the missing module.
If refresh fails in the Power BI service, that is expected for Python steps without a gateway. Install a personal gateway on a Windows machine that has Python and the packages, then bind the dataset to that gateway.
If everything worked yesterday and fails today, check for a Python upgrade that changed the interpreter path, and re-point Power BI at the new folder.
Frequently Asked Questions
Does Python in Power BI work on Mac?
No. Power BI Desktop for Mac does not support running Python scripts. You need Windows Desktop, or you can prepare data elsewhere and load the result.
Do viewers of my published report need Python installed?
For Python visuals, the rendering environment must have Python available; in practice this often means the report is viewed in Desktop or through a gateway-backed setup. Python script steps in Power Query run at refresh time, not when someone opens the report.
Can I use Python to create a custom visual?
You can use the Python visual to draw a chart from your data, but it produces a static image rather than an interactive Power BI visual. For interactivity, use built-in visuals or a custom visual built with the Power BI SDK.
Which Python version should I install?
A current Python 3 release from python.org is the safe choice. Avoid the Microsoft Store build, which Power BI often fails to detect.
Can Python scripts refresh automatically?
Only with a personal gateway on a machine that has Python and the required packages installed. Without it, the service cannot run the script.
Is my data sent anywhere when I run a Python script?
The script runs locally on your machine in Power BI Desktop, so the data stays on that machine during authoring. Be careful with any script that makes network calls, since that would send data outside your machine.