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

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?

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

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>")

Related

python run multiple scripts

Hi i would like to run add1.py and add2.py simultaneously and have searched BAT file and SH file but couldnt do it myself. Anyone can help me? The folder is in the following path C:\Users\Jia\Downloads\Telegram Bot\Scripts
I might also add more scripts like add3.py add4.py and the list goes on. Does anyone have simple tips that can help me run every script in this folder? Thank you!
It would be even better if the script runs one after another, example add2.py runs after add1.py finishes.
Just run: python add1.py & python add2.py. If you only want the second one to run if the first executes successfully, use python add1.py && python add2.py.
Running them at the same time would use something called concurrency, which would require some modifications to your script.
NOTE: This will only work on Windows. On Linux or MacOS, you would use: python add1.py ; python add2.py
You can manually add more scripts. To runn every python file in a folder, you could use: python *.py if you imported them all as modules into a new file called main.py and executed them in what ever order you like in that file.
As someone else already suggested, you could make a python file which executes your N python scripts.
Using subprocess as described here: https://stackoverflow.com/a/11230471/11962413
import subprocess
subprocess.call("./test1.py", shell=True)
subprocess.call("./test2.py", shell=True)
You can try this, if you are just tying to run python file:
import os
lst=[l for l in os.listdir() if l.endswith(".py")]
for ls in lst:
os.system(f'python {ls}')
Or if the name has some pattern or it is sure then, try this:
import os
for i in range(1,<up to last name+1>):
os.system(f"python add{i}.py")

Python Importing other Functions in Package

I have something like the following structure in my python package:
package/
--functions/
--functionsA.py
--functionsB.py
--utility/
--utils_printing.py
--utils_server.py
My goal is to be able to import package within any of the contained .py files in a way which works when I execute a specific function in one of the .py files from the command line.
Right now, if I want to use a server function in functionsA.py then I just throw a
from package.utility.utils_server import server_function
at the top of functionsA.py. Debugging with PyCharm, this works.
Now I'm trying to use pyheat to show me where my code needs to be optimized, but it requires that I run a given file as a script from the command line, but if I try to run pyheat functionsA.py then I'll get an error like ModuleNotFoundError: No module named 'package'.

Change the Windows folder permissions to Read only from Python

I have a test case to automate, where in I need to change the folder's permission to read only from Python script. I am using Test Complete tool with python scripting for automating the manual tests.
I am not able to accomplish the tasks, I tried Googling but no luck.
you can try this line of code.
import os, stat
os.chmod(ur"file_path_name", stat.S_IREAD)

Running Tensorflow script from another location

I have created Tensorflow image classifier and image classifier should be called using another python script.
I tried os.system() but I cant just call Tensorflow scripts coz it's depends on the multiple files in the Tensorflow script location. so I have to include all the files with classifier script in the main script(2nd python script).
What is the best way to do this?
when script is running :
script error when running from another location :
Do you have tried Python Subprocess instead of os.system?
From How do you use subprocess.check_output() in Python? you can see this simple demo:
py2.py:
import sys
print sys.argv
py3.py:
import subprocess
py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
print('py2 said:', py2output)
Running it:
$ python3 py3.py
py2 said: b"['py2.py', '-i', 'test.txt']\n"
UPDATE:
I think your problem it that you need to specify/set the correct folder path before running your python script. For example:
Consider you have the following folder structure:
/root/project/script.py
/root/project/data/file.txt
If in your python script you load a file using a relative path like ./data/file.txt, you need to run your script inside the project folder.
If you run it in the root folder, like this:
/root$ python project/script.py
the script fails, because it tries to find the data/file.txt inside the root folder.
You can use the cd command to change the current directory before running your python script. You can do it while calling the os.system, like this:
os.system("cd project && python script.py") # for my example case
whereever you call your python script from, all relative paths in your script will be based on. i.e. you called the script from its project folder in the first image, so the links to project_folder/tf_files/... were working, whereas you lateron called it from elsewhere, so that the symbolic links were messed up. your script tried to find elsewhere/tf_files/... but that subfolder does not exist.
you could edit the script so all paths are always abolute (starting with /home/...), then there is no way of confusing the

Can't run scripts through console in QPython

I'm using the app QPython, and while it's easy to run scripts from a file, I'm struggling to see how to load a script into the Console so that I can use it there (e.g. to use functions defined in a script).
I'm not very familiar with Python, so I don't know whether I'm having difficulty with Python or with the app. As far as I know in ordinary Python, the command "import script" will import all of the code in the file script.py, which has to be contained in the directory you loaded Python from (this is already concerning as I can't change the directory in QPython).
For the record, the equivalent command in Haskell (which I am familiar with) would be :l script.hs
To import some functions :
from script import functiona, functionb()
To import all functions from a script use :
from script import *
You could just do :
import script
But then you'll have to call your functions like that :
script.myfunction()

Categories

Resources