Forums

Trying to call another script from a script with python

Title explains it mostly. I am trying to call another python script from a python script. I am using:

@app.route('/lemay', methods=['POST'])
def view_do_something():

    if request.method == 'POST':

        #your database process here
        subprocess.call(['python', 'send_email_lemay.py'])

        return "OK"

When I run my flask server and send the post request I get an error. I am not sure why its not working? anybody got any ideas?

Thanks, DM123

Hi there

Make sure you are using the right version of python, eg "python3.6" if that's what you want

But -- is there a particular reason you're using a subprocess? why not, eg:

from send_email_lemay import send_email
[...]
# your database process here
send_email()

if you're attempting to use subprocess to get some kind of async/performance benefit, that's most likely to cause more problems than it solves. premature optimisation is the root of all evil, get it working the simple way first, etc etc :)

Harry, thank you for the responses. when i try to import send_email_lemay and run the main function it doesnt work. When i tried it offline it used to give the error that there was no attribute called main.

Here is the gist of the code I am using in the send_email_lemay script:

def main():
    monitor()

if __name__ == "__main__":
    main()

and here is the flask app im using to call send_email_lemay's main function

import send_email_lemay

@app.route('/lemay', methods=['POST'])
def view_do_something():

    if request.method == 'POST':

        #your database process here
        send_email_lemay.main()
        return "OK"

When i run python2.7 send_email_lemay from bash it works just fine. This is why i tried to go the subprocess route.

Thanks again for any help my man, DM123

The most likely cause of your import problems is that the modules you're importing is not the send_email_lemay.py that you think it is. See here for details about how Python finds modules and how you can debug import errors.

Thanks for the reply, Glenn.

It seems I dont have any issue importing and running main if I do it from console using python. Also i can run the wsgi file

when i run the wsgi file and import everything goes fine and its the correct path to send_email_lemay.pyc (im not sure if it being pyc matters?)

here is the output of checking the path

>>> import sys
>>> print('\n'.join(sys.path))
/home/daveyman123/competition
/var/www
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/Orange/orng
/usr/local/lib/python2.7/dist-packages/pypng-0.0.18-py2.7.egg
/usr/local/lib/python2.7/dist-packages/networkx-1.11-py2.7.egg
/usr/lib/python2.7/dist-packages
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode
>>>

Actually I may of gotten it to work. It now runs the script with the main() function when i call it.

However it doesnt seem my send_email script is working now.

Thanks Harry and Glenn for the help