No module named lxml while requesting the url from browser/curl - php

I have a python code which runs in my terminal and prints the result. I wrote a php code which executes the python code using exec("python example.py argument_1"). The php code also prints the result while executing it from the terminal.
But when I try to call the php from the browser (or) through a curl request, the result of the php is not being displayed. Apache error log displays the following message.
Traceback (most recent call last):
File "./example.py", line 3, in <module>
from lxml import etree
ImportError: No module named lxml
Kindly look into this issue.
Solution :
I had conflict with the python versions
exec("/usr/local/bin/python example.py argument_1") has resolved my issue

I think you can print the sys.path(modules will be fond by python via those path ) for more details. you maybe develop your code in linux.
In linux, each user has different sys.path.
for example, I use root to run python to get sys.path in python,
'/usr/bin',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/IPython/extensions']
use an other user to get sys.path in python
['',
'/home/mahome/anaconda2/bin',
'/home/mahome/anaconda2/lib/python27.zip',
'/home/mahome/anaconda2/lib/python2.7',
'/home/mahome/anaconda2/lib/python2.7/plat-linux2',
'/home/mahome/anaconda2/lib/python2.7/lib-tk',
'/home/mahome/anaconda2/lib/python2.7/lib-old',
'/home/mahome/anaconda2/lib/python2.7/lib-dynload',
'/home/mahome/anaconda2/lib/python2.7/site-packages',
'/home/mahome/anaconda2/lib/python2.7/site-packages/Sphinx-1.4.1-py2.7.egg',
'/home/mahome/anaconda2/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg',
'/home/mahome/anaconda2/lib/python2.7/site-packages/IPython/extensions',
'/home/mahome/.ipython']
so, you maybe use different user to execuate your code.

Related

Python package not imported when executed through PHP

I'm getting a import error for spacy when executing new_api.py through PHP. The python script gets successfully executed when run through the cmd
PHP Code
<?php
$json = file_get_contents('php://input');
$input = json_decode($json, TRUE);
$result = shell_exec('/usr/bin/python3 new_api.py "'.$input['queryResult']['queryText'].'" "'.$input['session'].'" 2>&1');
$output = json_encode(array(
"source" => "source",
"fulfillmentText" => $result
));
print_r($output);
?>
Snippet Of Python Code
import requests
import json
import sqlite3
import spacy
import sys
json_values = sys.argv[1:]
result = (home(json_values[0],json_values[1]))
print(result)
The Output/Error Traceback
{"source":"source","fulfillmentText":"Traceback (most recent call last):\n File \"new_api.py\", line 4, in \n import spacy\nImportError: No module named 'spacy'\n"}
There's two version of python in the server. Both the versions has spacy installed in it and the python script is compatible with both versions.
Things I've tried
Tried using exec() and shell_exec() in php
Tried using which python path to execute the script
Tried running the script which both python and python3
The ImportError Still persists even after trying the combinations of the above steps. Any help would be appreciated.
Probably the user of the server who execute the PHP script does not have enough permissions to access the module.
You can install the module as server user or use venv
I have same problem, and I soleved it just a moment ago. This question is old, but I leave a note for people has same problem, and for me.
"printenv" on shell (your own user).
exec('printenv') from php (probably "apache" user).
Check difference between 1 and 2 results.
In my case, LD_LIBRARY_PATH=/usr/local/pgsql/lib is needed (exist in 1 and not exist in 2). So I modified my code as follows:
exec('LD_LIBRARY_PATH=/usr/local/pgsql/lib python3 test_script.py 2>&1');

How to run .php in python?

I have the following python script test.py:
import subprocess
subprocess.call(["php", "C:/Documents and Settings/user/My Documents/Downloads/load_data.php"])
the idea is to run another php script in which I make a LOAD DATA INFILE to a MySQL table, but it generates the following error:
Traceback <most recent call last>:
File "test.py", line 3, in <module>
subprocess.call<["php","load_data.php"]>
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen<*popenargs, **kwargs> as p:
File "C:\Python34\lib\subprocess.py" line 859, in __init__
restore_signals, start_new_session>
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo>
FileNotFoundError: [WinError 2] The system cannot find the file specified
If I run the direct load_data.php script in a web browser, it works without any problem so it seems to me that the problem is in python, it is worth mentioning that I have done this correctly before but with python 3.6, some idea if I need it configure something or add a library.
I have php5.3, apache 2.2.21, mysql5.5.20, python3.4, I tried to put the absolute path of the PHP script but is the same
Regards!
For run PHP with Python, you must add the full path to the PHP executor and the full path of the file to execute.
Example :
import subprocess
subprocess.call(["C:\\wamp64\\bin\\php\\php5.3\\php.exe", "C:\\Documents and Settings\\user\\My Documents\\Downloads\\load_data.php"])
Don't forget to escape the anti-slashes, Otherwise the subprocess will not work and there will be no visible error.

Accessing python nltk with php fails

Im trying to call a python file containing a sentence/word tokenizer from my php file like this:
$output = shell_exec('python tokenizer.py $sentence')
I've tried single exec, full paths to python and tokenizer.py, wrapping $sentence in double quotes. But logically, It should not be the problem because calling print(1) at the beginning of python the python code before actually using any nltk packages makes $output equal to '1'. So I came to conclusion that the problem here is the nltk itself, like the path to the modules is not correct or something...
But, calling python from the shell using the same command as above gives me fully tokenized output! To conclude: looks like when calling python from php magically 'turns off' nltk, while it fully works when executed from the shell.
Here's the part of the python code I am using:
import sys
import nltk
from nltk.tokenize import sent_tokenize
sample_text2 = sys.argv[1]
gust = sent_tokenize(sample_text2)
#print(1) here doesn't work, but everywhere above (before calling sent_tokenize) it does.
The server's running on CentOS (Linux), I am accessing it via SSH.
Obvious question: What am I doing wrong here with PHP? Or generally? Any alternatives?
EDIT
As visible in dvhh's answer and its comments, the situation happened because there were two versions installed on the server (2.6 and 2.7), while the www user had access to 2.6 and through console, the default version was 2.7. The solution was to change the default python to 2.7 for both cases and to put the nltk modules to one of the dependency folders. (Or append the dependency directory using sys.path.append)
Your php script is executed by the www user.
You could check if the python script interpreter is correctly called, it is usually in one of the directory in the PATH environment variable (like /usr/bin/python), but the www user don't have a PATH environment variable set.
Solution specify the whole path to your python interpreter in your shell_exec call ( also specify the full path to your script when you're at it )
What about the path the nltk library is installed, you could check if the python interpreter would correctly look for it by looking at the sys.path while running python with the www user.
Diagnostic : use the shell_exec call to run a python script to print the sys.path values
Solution : append the library path to the sys.path in your python script before the import nltk
These would be the most obvious solutions considering the information provided in the question.
Update :
As there is 2 version version of python installed (on that haven't got the library installed ), it is recommended to specify the path to the desired interpreter. The first solution help correct the issue.
In unix like system I would recommend using which python command to determine the path of your default python interpreter.
Option 1
Setup a simple python httpserver listening on localhost. This old answer might help but there are plenty of howtos out there. The advantage is that you don't have the overhead of starting the python interpreter each time the ntlk stuff needs to be executed and you don't have to worry about shell script executions, permissions etc. Disadvantage is a little of extra work and a little overhead.
Option 2
Using a task queue. Whatever said and done it's not safe to execute commands from your web facing PHP scripts. If you are already using RabbitMQ or something similar you can use that here. Or else if you are using redis you can use the lpush, rpop methods to make redis behave like a queue. Disadvantage: the result is not immidiately available.
Option 3
Anbother strategy for your php script to enter the data into a table and setup your python script to run as a cron job to check the table once a minute. Disadvantage: the result is not immidiately available.
Option 4
Your current choice but please make sure that you have escaped the data properly by #lafor if this option is chosen #dvhh 's answer ought to work.
In my case it wasn't problem of python version but problem (as I soon found) of the nltk_data folder. First I thought it was as permission problem, so I changed the permissions to 777, but that didn't work.
I had to copy the folder.
First you have to find where was the nltk folder installed. You will find it by running python3 command from your bash and then put following lines:
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
It will say something like:
[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data] Package punkt is already up-to-date!
Now you know you have it installed eg in /root folder (i had it there).
Then put in your python script which is called by php this:
import nltk
print(nltk.data.path)
It will output something like: (note the folders are another if you run it from php script or if you call it from command line)
['/var/www/nltk_data', '/usr/nltk_data', '/usr/share/nltk_data', '/usr/lib/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data']
Now just copy your original folder to any of the above folders. I did:
cp /root/nltk_data /var/www/nltk_data -r
Voila. It works now.

relocation error on running python script from php

I'm trying to run my python script from php. I get the following error
python: relocation error: python: symbol SSL_load_error_strings, version OPENSSL_1.0.0 not defined in file libssl.so.1.0.0 with link time reference
I'm using import socket in python script which is causing the error.
Any help with would be much appreciated.
php_code -
shell_exec('python C:/xampp/htdocs/webInterface/php_ex/pyth_1.py aa 2>&1');
Xampp comes with it's own version of the common libraries.
When you run Python from PHP, the environment variables are set to find Xampp libs, and their versions might differ from what Python is expecting.
On unix, to see what is different run from PHP:
shell_exec('/usr/bin/env');
Then compare the output of the same command from a terminal window.
For me, unsetting the LD_LIBRARY_PATH was all I needed to get Python working:
shell_exec("LD_LIBRARY_PATH='' python myscript.py");

PHP exec python not working

hey yall. Im running python on a webserver from dreamhost. I am using their install of python and am using a lastfm module that can be found here: http://code.google.com/p/python-lastfm/
to get it to import properly i do this
import sys
sys.path.append("/home/myusername/build/Python-2.5/Lib/site-packages/")
import lastfm
since the lastfm module is installed there.
When I use putty to ssh into my server, i can simply run python test.py and it works perfectly. But when i run it from a php script with
exec("python test.py");
it suppossedly does not work and the script doesnt run. it runs perfectly fine when i do
import lastfm
and then have other things after,
but when i actually try to do something with the module like:
import lastfm
api=lastfm.Api(api_key)
it does not run. once again i can run the script using the same python install in a shell and it executes fine. So something must be happening that goes wrong when i run it from the php script. I figured it would be running the exact same python and everything. I checked other posts and they say it may be something with file permissions, but ive put every file to 777 and it still doesnt work. idk what the problem could be. thanks in advance everyone.
Try using the full path to the python executable. For example:
exec("/usr/bin/python test.py")
You can find the full path from the command line using the which command:
$ which python
/usr/bin/python
Whatever error python is raising would be going to the child's stderr. Try either telling php to read from stderr, or (in python) do this:
import sys
sys.stderr = sys.stdout
For Windows users:
$output = null;
exec('C:\\Python27\\python.exe C:\\sud.py', $output);
echo var_export($output, TRUE);
The code i was searching whole day ^^
That's why - hope it'll help somebody.
For Windows User -
Thanks to Karlisup my PHP file could read python.
I'm using BITNAMI WAMP in EC2 Amazon, my python file (leadatos.py) and php file are on htdocs folder.
My calling was
<?php
passthru('C:\\Python27\\python.exe leadatos.py');
?>
The last line of my Python file was print "message".
Hope it words!

Categories