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['python', ''])

        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

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():

if __name__ == "__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
        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 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))

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