0

I am able to easily call a python script from php using system(), although there are several options. They all work fine, except they all fail. Through trial and error I have narrowed it down to it failing on

import MySQLdb

I am not too familiar with php, but I am using it in a pinch. I understand while there could be reasons why such a restriction would be in place, but this will be on a local server, used in house, and the information in the mysql db is backed up and not to critical. Meaning such a restriction can be reasonably ignored.

But how to allow php to call a python script that imports mysql? I am on a Linux machine (centOs) if that is relevant.

matchew
  • 19,195
  • 5
  • 44
  • 48
  • What's the error message. Is it like in [this question](http://stackoverflow.com/questions/4380931/installing-mysql-python-on-centos)? – Aya Apr 29 '13 at 14:58
  • @Aya No, I am able to run the script properly if I run it as myself, or root. but not as apache user. (www-data in some distros). In my original post I mention I am not familiar with php. I use system and it returns a one. How do I capture stderr? – matchew Apr 29 '13 at 15:00
  • Looks like `system()` only returns one line from the output. Maybe something like `passthru('sh -c "python scriptname.py 2>&1"')` would work? – Aya Apr 29 '13 at 15:05

1 Answers1

1

The Apache user (www-data in your case) has a somewhat restricted environment. Check where the Python MySQLdb package is installed and edit the Apache user's env (cf Apache manual and your distrib's one about this) so it has a usable Python environment with the right PYTHONPATH etc.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118