4

I have a PHP script, that calls a python script by

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

This produces me an error in the log:

$ 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 do I miss?

Edit:

After the suggestion of @S.Lott I had a look at the variables PATH and PYTHONPATH both in the terminal and in PHP.

In the 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 is 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 are

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

I am open for suggestions. =)

Edit2:

I checked every installed python version on my mac. I found out, that python2.7 has no MySQLdb installed. Is there a way to tell PHP not to use python2.7 and to use e.g. python2.6 instead? I tryed toying with setenv() in PHP but I couldn't figure out how to use it properly, and I don't even know if this is the right approach.

13
  • 2
    In PHP, your PYTHONPATH is different from in the shell. Or, worse, the entire PATH is different and you have multiple Pythons. You need to display the PATH and PYTHONPATH environment variables at the command prompt and in PHP. There may be other things, but that's a start. Commented May 18, 2011 at 22:24
  • If I may ask, how do I do that in PHP? Commented May 18, 2011 at 22:25
  • Yeah...I just did a test on my system and it works without specifying PYTHONPATH from PHP. I'm betting on multiple installs here, one without MySQLdb. Commented May 18, 2011 at 22:26
  • @Aufwind: That's a separate question. Step 1. Search. Step 2. If that doesn't help, Ask. Commented May 18, 2011 at 22:27
  • @Aufwind Also be careful with your current working directory, if your importing modules relative to your lp_3.py file. Commented May 18, 2011 at 23:17

2 Answers 2

4

In your PHP code, you're just calling "python", and letting PHP decide which version of Python to use. Use an explicit path to a specific Python binary, (e.g. /usr/bin/python2.6).

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

Sign up to request clarification or add additional context in comments.

Comments

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

did the job. Like @AJ pointed out, I had to tell python which version to chose. I chose a version where MySQLdb was available and all was fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.