Home » Php » python – What is the difference between running a script from the command line and from exec() with PHP?

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

Posted by: admin July 12, 2020 Leave a comment


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)
  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'
How to&Answers:

Your web server likely runs with other privileges than yourself. Possible problems include:

  • Path/file permission: can the web server user access the files it needs?
  • Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
  • Configuration: are there per-user configurations for Python or the module?

Tip: execute set in both the command prompt and from the PHP process and check the differences.


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

import nltk

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.


From the shell/terminal, you can use:

sudo python -m nltk.downloader maxent_treebank_pos_tagger

It will install maxent_treebank_pos_tagger (i.e. the standard treebank POS tagger in NLTK).