从php调用python脚本而不导入所需的模块

时间:2015-12-18 12:52:23

标签: php python ampps

我正在使用这个

在php中调用python脚本
$text = shell_exec("python import pdf2txt; parser_py/cv_parser.py -i '".$filepath."' 2>&1");

请注意,$ filepath已在前面声明,这不是问题。

这是python脚本

import textract, os, sys, getopt

def main(argv):
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         sys.exit()
      elif opt in ("-i"):
         print textract.process(arg)

if __name__ == "__main__":
   main(sys.argv[1:])

textract导入其他模块,当我运行php时它们会失败但是当我通过终端运行时工作正常。这是下面的错误

string(1635) "Traceback (most recent call last):
  File "parser_py/cv_parser.py", line 39, in 
    main(sys.argv[1:])
  File "parser_py/cv_parser.py", line 32, in main
    print textract.process(arg)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/textract-1.4.0-py2.7.egg/textract/parsers/__init__.py", line 57, in process
    return parser.process(filename, encoding, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/textract-1.4.0-py2.7.egg/textract/parsers/utils.py", line 44, in process
    byte_string = self.extract(filename, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/textract-1.4.0-py2.7.egg/textract/parsers/pdf_parser.py", line 25, in extract
    return self.extract_pdfminer(filename, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/textract-1.4.0-py2.7.egg/textract/parsers/pdf_parser.py", line 49, in extract_pdfminer
    stdout, _ = self.run('pdf2txt.py "%(filename)s"' % locals())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/textract-1.4.0-py2.7.egg/textract/parsers/utils.py", line 92, in run
    command, pipe.returncode, stdout, stderr,
textract.exceptions.ShellError: The command `pdf2txt.py "/Applications/AMPPS/www/insidify_cv_training/files/2musa_bello.pdf"` failed because the executable
`pdf2txt.py` is not installed on your system. Please make
sure the appropriate dependencies are installed before using
textract:

    http://textract.readthedocs.org/en/latest/installation.html

"

我在mac上使用AMPPS服务器。我该如何使这项工作好吗?

0 个答案:

没有答案