Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYTHONPATH and PHP

I have a PHP script that call a python script (someone else I can't edit). This work fine on CLI but once I run it as CGI it fails with the error "bzr: ERROR: Couldn't import bzrlib and dependencies."

I guessed it has anything to do with PYTHONPATH so I echo it out and found that it was empty when in apache environment.

I try setting it in my '.htaccess' (I am on Dreamhost so I can't set httpd.conf) as:

SetEnv PYTHONPATH "/home/nawaman/lib/python"

It still does not work.

What can I do about this? Thanks in advance for any help.

like image 259
NawaMan Avatar asked Jul 13 '10 15:07

NawaMan


People also ask

Can we connect PHP to Python?

Practical Data Science using PythonTo call a Python file from within a PHP file, you need to call it using the shell_exec function.

What is Pythonpath used for?

It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the name of various directories that need to be added to the sys.

What is PythonPath Windows?

The PYTHONPATH environment variable is used by Python to specify a list of directories that modules can be imported from on Windows. When running, you can inspect the sys. path variable to see which directories will be searched when you import something.


1 Answers

As far as I know, Apache's environment variables don't correlate with the system ones.

Try setting PYTHONPATH using PHP's putenv()

Alternatively, I don't know much about Python, but it seems to be possible to set the path inside a Python script using sys.path maybe that helps.

like image 80
Pekka Avatar answered Sep 17 '22 09:09

Pekka