python import fails when called from PHP

前端 未结 4 651
长情又很酷
长情又很酷 2020-12-21 00:17

I\'m having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.

From the python shell:

         


        
相关标签:
4条回答
  • 2020-12-21 00:59

    Is the igraph module in Python's standard module path, or is it in the same directory as your individual script? If so, it's quite possible that PHP is calling the python file with a different working directory, and it's trying to import things relative to that path instead of the path of the script.

    0 讨论(0)
  • 2020-12-21 01:04

    one thing to check is sys.path

    see what the difference is when called each way

    0 讨论(0)
  • 2020-12-21 01:11

    This is happening because you have installed those packages under a different user, maybe root, or something else.

    How i debugged this, is i checked the output of sys.path for both cases (shell, and php's exec, which has the user www-data by default), and than i compared both.

    I noticed the '/root/.local/lib/python2.7/site-packages' path missing when i ran it from PHP, which contained exactly these missing packages. So i just copied the content of this folder to '/usr/lib/python2.7/dist-packages/', which solved the issue.

    0 讨论(0)
  • 2020-12-21 01:12

    This could be due to the following reason.

    When you import a package, certain directories are searched. These directories are specified inside sys.path

    Create a python file with the following lines.

    import sys
    print(sys.path)
    
    • Run this file in the shell and note down the output(list of directory paths).
    • Run the same file in the local server(using PHP) and compare the outputs.

    The path missing in the PHP output is probably the one containing the package. Therefore, add the following lines in the python file you are running on the server.

    import sys
    sys.path.append("path-to-the-missing-directory")
    

    RUN IT.

    0 讨论(0)
提交回复
热议问题