'Import local ".py" library in powerbi python scripting

I have written a python library (ak_sql.py) to query my sql server and written another ".py" script (query_sql.py) to invoke this library and return data in dataframes.

I want to "import query_sql" in "Python srcipt" in PowerBI, so i can load those dataframes.

I invoked PowerBi from powershell from the root of "query_sql", like below:

C:\Users\akshat\Documents\ak_sql> C:\Users\akshat\AppData\Local\Microsoft\WindowsApps\PBIDesktopStore.exe

However, PowerBI was unable to find the module.

I tried printing "os.getcwd()", this is the output:

C:\Users\akshat\PythonSriptWrapper_{hashkey}

Any inputs on how this can be done?



Solution 1:[1]

So seems like it was a PYTHONPATH issue. Was a able to take care of it with appending sys.path:

import sys
sys.path.append(r"C:\Users\akshat\Documents\ak_sql>")

Solution 2:[2]

Try to include the empty file __init__.py in the ak_sql directory and make change in your script on

from ak_sql import query_sql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akshat
Solution 2 Mozgawa