Difference between python script terminal call and PHP? Where is the mistake?

I have a PHP script that calls a python script on

$call_python = "python ../python/lp_3.py ".$author; $python_output = Null; $mystring = exec($call_python, $output_python); 

This results in a log error:

 $ vi logs/error_log shows .... Traceback (most recent call last): File "../python/lp_3.py", line 14, in <module> import MySQLdb ImportError: No module named MySQLdb 

If I do python python/lp_3.py in the terminal, everything is fine. What am I missing?

Edit:

After the @ S.Lott sentence, I looked at the PATH and PYTHONPATH variables in both terminal and PHP.

In terminal:

 $ echo $PYTHONPATH $ echo $PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/texbin:/usr/X11/bin 

As you can see, PYTHONPATH empty.

In PHP:

 echo getenv("PYTHONPATH"); // NOTHING echo getenv("PATH"); // /usr/bin:/bin:/usr/sbin:/sbin 

Perhaps I should mention that the first two lines in my python script:

 #!/usr/bin/env python # encoding: utf-8 

I am open to suggestions. =)

Edit2:

I checked every installed python version on my mac. I found out that python2.7 does not have MySQLdb installed. Is there any way to tell PHP not to use python2.7 and use for example. python2.6 instead? I tried playing with setenv() in PHP, but I couldn’t figure out how to use it correctly, and I don’t even know if this works correctly.

+4
source share
2 answers

In your PHP code, you simply call "python" and let PHP decide which version of Python to use. Use the explicit path to the specific Python binary (e.g. / usr / bin / python2.6).

You need to know the exact path to the version of Python with MySQLdb installed.

+4
source
 $call_python = "/opt/local/bin/python2.6 ../python/lp_3.py ".$author; $python_output = Null; $mystring = exec($call_python, $output_python); 

performed this work. As @AJ pointed out, I had to tell python which version to choose. I chose the version where MySQLdb was available and everything was fine.

0
source

All Articles