Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between running a script from the command line and from exec() with PHP?

I'm trying to run a Python script using exec() from within PHP. My command works fine when I run it directly using a cmd window, but it produces an error when I run it from exec() in PHP.

My Python script uses NTLK to find proper nouns. Example command:

"C:\Python25\python.exe" "C:\wamp\projects\python\trunk\tests\find_proper_nouns.py" "I went to London this morning" 

returns [London] when I run it from cmd, but throws an error in the Apache log when I run the same command from exec().The script is defintely getting run OK - if I change the python script to be print "Hello World" that is returned fine.

I know it's a big ask for anyone to know how to fix this NLTK error, but I could really do with any pointers as to why running it from exec is different to cmd. (The command is identical).

I'm running WAMP on Windows 7 with Apache 2.2.11.

Here's the error in the Apache log:

Traceback (most recent call last):
  File "C:\wamp\projects\python\trunk\tests\find_proper_nouns_command_line.py", line 6, in <module>
    parts = nltk.pos_tag(text)
  File "C:\Python25\lib\site-packages\nltk\tag\__init__.py", line 62, in pos_tag
    tagger = nltk.data.load(_POS_TAGGER)
  File "C:\Python25\lib\site-packages\nltk\data.py", line 590, in load
    resource_val = pickle.load(_open(resource_url))
  File "C:\Python25\lib\site-packages\nltk\data.py", line 669, in _open
    return find(path).open()
  File "C:\Python25\lib\site-packages\nltk\data.py", line 451, in find
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
  found.  Please use the NLTK Downloader to obtain the resource:
  >>> nltk.download().
  Searched in:
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'C:\\Python25\\nltk_data'
    - 'C:\\Python25\\lib\\nltk_data'
    - 'C:\\Windows\\system32\\config\\systemprofile\\AppData\\Roaming\\nltk_data'
**********************************************************************
like image 207
Roy Avatar asked Feb 18 '10 13:02

Roy


People also ask

What is the difference between exec and Shell_exec?

The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.

What is exec command in shell script?

The exec command is a powerful tool for manipulating file-descriptors (FD), creating output and error logging within scripts with a minimal change. In Linux, by default, file descriptor 0 is stdin (the standard input), 1 is stdout (the standard output), and 2 is stderr (the standard error).

What does exec command do in Linux?

The Linux exec command executes a Shell command without creating a new process. Instead, it replaces the currently open Shell operation. Depending on the command usage, exec has different behaviors and use cases.


1 Answers

You have to run nltk.download() and choose 'maxent_treebank_pos_tagger'. You must make a python script and in it put:

#!/usr/bin/python
import nltk
nltk.download('maxent_treebank_pos_tagger');

then run it from command line. It will install the data files for the POS tagges, which you don't have installed yet.

After you do this it should work.

like image 182
Xelo Avatar answered Sep 28 '22 11:09

Xelo