简体   繁体   中英

Can I call a function from a personal Python Scripit in Power BI

In Python Script option inside Power BI, I would like to use:

from personal_script.py import function

In order to use some personal functions to do some data transformation inside Power BI

Is there a way of doing this?

I have tried:

from personal_script.py import function

I expect the output of being able to use my personal function collected in my personal Python Script, but I got:

Unable to connect
We encountered an error while trying to connect.
Details: "ADO.NET: Python script error.
Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 9, in <module>
  from personal_script import function
ImportError: No module named 'personal_script'

https://community.powerbi.com/t5/Desktop/Can-I-call-a-function-from-a-personal-Python-Scripit-in-Power-BI/mp/813584#M391150

I had the same problem today and I was able to solve it by doing something like this:

import importlib.util
spec = importlib.util.spec_from_file_location("folder.module", r"C:\Users\USER\PATH_TO_FILE\FILE.py")
module = importlib.util.module_from_spec(spec)
import pandas as pd
spec.loader.exec_module(module)

module.Class()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM