Search code examples
pythonmysqldjangosubprocesspymysql

Calling python from django subprocess, mysql not found error


  1. python dbtest1.py ==> work O.K.
       dbtest1.py :<br>
       import pymysql.connector<br>
       dbCon = pymysql.connector.connect(host='...', database='...', user='...', password='...')<br>
       cursor = dbCon.cursor()<br>
       cursor.execute("INSERT INTO cm_person (name) VALUES ('고길송')")<br>
       dbCon.commit()
  1. access from Django using subprocess, Not found error...

   views.py include...

    def datatest(request):<br>
       subprocess.call(['python', 'dbtest3.py'])<br>
       return HttpResponse('Call python...')

Error message;

     ModuleNotFoundError: No module named 'pymysql'

Did I miss something? or is there any other methods? Thank you.


Solution

  • If you are using virtual environments, depending how you run your script, 'python' might refer to the system Python. Using sys.executable instead of 'python' might help.

    Note that if you are using WSGI, sys.executable is probably not set correctly, so you might want to set it explicitly in your WSGI entry point script.