Bruno do Val
Bruno do Val

Reputation: 37

Execute stand alone python script inside another script

I have made a .py file (i call it acomp.py) that reads from and SQL database and after a series of calculations and exports its output to an excel file.Later I sends this .xlsx file by e-mail to various persons.

I wish to put it inside another python script so I can use the schedule module to call acomp.py aromatically at selected times, run it and send the output by email:

def exec_scrpit():
    exec(open("acomp.py", encoding='utf-8').read())
    send_email()

schedule.every().day.at("07:20").do(exec_scrpit)

When the second script calls the fist it messes with the acom.py internal functions returning the error:

"  File "<string>", line 125, in <lambda>
NameError: name 'modalidade_contrato' is not defined"

This 'modalidade_contrato' is defined inside acomp.py and it runs perfectly when I execute acom.py directly.

Any ideia how should I proceed? I think my whole strategy is not usual, but I have to do it this way because I do not have admin privileges on my computer.

Upvotes: 0

Views: 346

Answers (1)

Bruno do Val
Bruno do Val

Reputation: 37

I learned that you can simply run it as you would in cmd:

os.system(r'"C:\Users\user.name\Miniconda3\python.exe acomp.py"')

Upvotes: 1

Related Questions