0

I have a Python script that in the middle of it will have a function where I want to query a DB table and run whatever Python scripts are listed in one of the columns. The Python scripts themselves reside in the same folder as the main Python script that is being executed. For specific reasons I need to keep these script names in a DB table though and call/read them from there, hence my issue.

python_script_table in DB looks like:

TABLE_ID    PYTHON_SCRIPT
1           script1.py
2           script2.py
3           null

Query would be something like:

select * from python_script_table where python_script is not null

At that point I want to execute whatever is returned under PYTHON_SCRIPT (in this case script1.py and script2.py).

I am unsure the best way to approach this..

7
  • 1
    You could also store the path to the script and then call the script from the subprocess module. Commented May 2, 2019 at 18:02
  • storing the path of the script, instead of the script name is also fine. Thanks Bernie. I will check out the subprocess module Commented May 2, 2019 at 18:03
  • 1
    Other alternatives would be to open and read them into a string and then either exec() it or "import" it using the __import__() functions. Commented May 2, 2019 at 18:10
  • thanks @martineau...so something like this would work? exec(/path/to/file/script1.py) ? Commented May 2, 2019 at 18:11
  • 1
    No, you would need to with open('path/to/script.py') as file:, script = file.read(), then exec(script). Both exec() and __import__() execute the script within the current process, not as a separate process—which may or may not be desirable depending on what you're trying to accomplish. Commented May 2, 2019 at 18:14

1 Answer 1

1

You should be able to execute the scripts with something like this:

with open('path/to/script.py') as file:
    script = file.read()
    exec(script)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.