1

I am trying to simplify a workflow that requires several individual scripts to be run. So far, I have been able to write a script that runs the other scripts but I have one issue that I can't seem to resolve. Each of the sub-scripts requires a file path and one argument within the path needs to be changed depending on who runs the scripts. Currently, I have to open each sub-script and manually changing this argument.

Is it possible to set this argument to a variable in the parent script, which can then be passed to the subscripts? Thus, it will only need to be set once and will no longer require it to be updated in each sub-script.

So far I have.....

import os

def driver(path: str):
    path_base = path
    path_use = os.path.join(path_base, 'docs', 'analysis', 'forecast')
    
    file_cash = os.path.join(path_use, 'cash.py')
    file_cap = os.path.join(path_use, 'cap.py')

    exec(open(file_cash).read())
    exec(open(file_cap).read())

    return

if __name__ == '__main__':
   driver(path=r'c:\users\[username]')

I would like to set path=r'c:\users\[username]' and then pass that to cash.py and cap.py.

5
  • Couldn't you just use Microsoft's built in scheduler to run the scripts you need in that order? How many scripts are you going to end up running with your parent script? Commented Aug 26, 2021 at 19:14
  • Can you pass the argument as a command line argument using sys.argv? Commented Aug 26, 2021 at 19:15
  • 2
    Python has better mechanisms to execute code that resides in one module from an other module. Have you thought about wrapping everything that a certain 'script' does into a function, import your script (as a module) and execute that function? Btw, function can take parameters, which is approximately 'setting variables' by the parent. Commented Aug 26, 2021 at 19:18
  • You should look at importlib module, not exec() function Commented Aug 26, 2021 at 19:23
  • Austin - This is a basic example of what I am attempting to do. There are more scripts, and those scripts run scripts. The biggest issue is setting the path across all of the scripts as it needs to change based on who is running them. Commented Aug 26, 2021 at 19:30

1 Answer 1

1

Instead of trying to replicate the behaviour of the import statement, you should directly import these subscripts and pass the values you need them to use as function / method aruments. To import a script from a specific path, you can use importlib.import(), like this:

main.py

import os

def driver(path: str):
    path_use = os.path.join(path, 'docs', 'analysis', 'forecast')
    
    file_cash = os.path.join(path_use, 'cash.py')
    file_cap = os.path.join(path_use, 'cap.py')

    importlib.import(file_cash)
    importlib.import(file_cap)
    
    cash.cash("some_arg")
    cap.cap("some_other_arg")

if __name__ == '__main__':
   driver(path=r'c:\users\[username]')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.