I'm setting up a big system relying on python 2.7 being run through php. The call is always something like:
exec('python test.py');
However no matter what I do PHP keeps using python 2.4 for executing my files. Because of the size of the system I can't change in the programming, but will have to make 'python' point directly to python2.7.
By searching around I have reached the conclusion that I should change the php env.
echo getenv("PYTHONPATH"); // NOTHING
echo getenv("PATH"); // /bin:/usr/bin
I can do so through putenv (for example: putenv("PATH=/usr/bin/python2.7:".$_ENV["PATH"]), but php keeps running python 2.4 no matter what I change it to.
Hope somebody out there got a simple solution :)
Could you not just do this instead:
exec('/usr/bin/python2.7/python test.py');
another option, you can set path to interpreter in 1st line of script test.py
#!/usr/local/bin/python2.7
but you need make test.py executable
chmod +x path_to_file/test.py
and run from php as
exec('path_to_file/test.py');
P.S. be attentive administrators sometimes disable exec function on servers for safety.
disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....
If you can't use full path, try an alias:
alias python='/usr/bin/python2.7'
python --version
Python 2.7.2
Related
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.
I would like to be able to either launch php in interactive mode via phing or via PHP (worst case scenario, phing can run an adhoc task with the desired code).
I've gotten this far:
<?php
$cmd = 'php -d auto_prepend_file=bootstrap.php -a';
passthru($cmd)
And the above almost gets me what I want. I enter a shell I can interact with, but the prompts are gone (the php > at the start of each line), all meta-commands (e.g., \>) totally fail, and typing exit; does nothing instead of exit the shell. So, as you can see, this isn't the ideal shell. Is this even possible without installing phpsh?
Thanks in advance.
I think PsySH will give you want you want. It's a PHP REPL that gives you the option to configure it to automatically include a bootstrap file.
I have a PHP CLI script that I invoke using
php application.php --args etc
However I would like to alias the script so that I can just execute the script without prefixing the command line call with php and having the '.php' extension.
application --args etc
Is this possible? I pressume it is but lack the knowledge or probably the correct terms to search for in Google.
You need to do the thing that Mike Brants says add the next line to your sample.php file
#!/path/to/cli/php
but also you have to do these in linux
chmod +x sample.php
To tell the linux (unix) machine to interprete these file as an excecutable
You could just use a shebang to define the application to use for execution from within the file. So at the beginning of your script you would place something like this:
#!/path/to/cli/php
<?php
// start your PHP here
When executed from command line the OS will know to use the specified PHP CLI application to execute the script. Obviously the path to the PHP CLI excutable will vary based on your system and should be substituted with what I have shown above.
This is more flexible that aliasing IMO, as you don't need to enter an alias for each PHP script you may want to run in such a manner from the command line.
ahah. alias can be added to the .base_profile
http://www.hypexr.org/bash_tutorial.php#alias
Use a so called 'shebang':
In the first line of your script add:
#!/usr/bin/php
where /usr/bin/php is the path to your php cli executable.
That's it !
Even more logical and pleasant (at least my favorite) call is #!/usr/bin/env php
Quote part from the user contributed note on PHP manual it self:
uses "env" to find where PHP is installed: it might be elsewhere in the $PATH, such as /usr/local/bin.
<?php
exec("whoami");
?>
I can be more explicit with the code . Although When I'm trying to call the php file with my browser nothing happens (of course I'm using apache and the whole).
Note : The safe_mode is activated, I'm using php5, php interpreter seems to be nice when running other functions, I'm a ubuntu user.
Then what is wrong?
I think you're looking for the echo function. Executing whoami using the exec function will run the program but show you nothing… you want to spit out the result too.
echo exec("whoami");
You have to echo the output of the exec command somewhere.
PHP documentation for exec function contains the example with whoami, look at the echo.
Right in the docs for exec:
When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.
If possible, turn off safe mode. Safes you lots of headaches.
Otherwise, is the php file owned by the same user that Apache runs as?
On Ubuntu, this will usually be www-data.
Try:
sudo chown www-data /path/to/you/script.php
Then run again.
I have a program that returns a comma-separated string of numbers after doing some background processing. I intended to run this in symfony using shell_exec; however, all I get is NULL (revealed through a var_dump(). I tried the following debugging steps.
I ran the file (it's a PHP class) through a command-line lime unit test in Symfony - it works and gives the correct result there.
Just to check, I tried a simple command ls -l at the same place to see whether I would get anything. Again, I had the same problem - the var_dump in the browser showed NULL, but it worked through the command line.
What could be the problem? Are there restrictions on running shell_exec() in a browser?
EDIT: Just to clarify, shell_exec() commands work when I run them as standalone php scripts on the web server (for example, by putting them in my document root. They don't seem to be working under the symfony framework, for some reason.
I finally solved it, and it turned out to be something quite simple, and quite unrelated.
The shell command I was running was in this format: face_query -D args. I didn't realize that Apache would be executing PHP as user www-data and thus the program face_query wouldn't be in the PATH (the directory is actually ~/bin). Changing the program name to the full path of the program solved it.
I also gather from this that only commands which www-data has permission to execute can be run. In this case, www-data is in the same group as my user, but it might be a problem otherwise.
Have you tried using exec? Or one of the other variants. I am never sure of which one to use and always lump with exec.
http://uk.php.net/manual/en/function.exec.php
Is your web server running php in safe mode?
Note: This function is disabled when PHP is running in safe mode.
From: http://php.net/manual/en/function.shell-exec.php