0

I'm trying to run a python script from php with

$res = '';
exec('./home/Desktop/Scripts/fetch_matches.py', $res);

My python file starts with #!/usr/bin/python and has exec rights. For now I only have a print in there but it's not working(var_dump($res) gets me an empty array). What's missing?

Also, if I'll have different methods in that script, how would I call them?

5
  • 1
    Correct relative path? Have you enabled error_reporting? Commented Aug 3, 2015 at 7:21
  • My python file starts with #!/usr/bin/python and has exec rights exec rights for ???* and second to check is the php user/group able to go in the home directory? Commented Aug 3, 2015 at 7:23
  • Yes, that's the path pwd gives me and I think it's enabled since I got errors for other parts of the program Commented Aug 3, 2015 at 7:25
  • can you execute it from terminal ? That PY file? I mean direct execution? Commented Aug 3, 2015 at 7:34
  • yes, I can execute it from terminal Commented Aug 3, 2015 at 8:34

2 Answers 2

1

If you change your exec to include /usr/bin/python this should work as expected:

exec('/usr/bin/python ./home/Desktop/Scripts/fetch_matches.py', $res);

In these circumstances you should probably use absolute paths (check realpath as well as chdir and any argument escaping you need to do).

I'm not completely familiar with how the shebang in files is run but I know if calling from PHP it's far better just to include the interpreter in the command you run.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to get rid of the . before the path. If it's a normal Unix/Linux system, /home is in the root of the filesystem. . means the current directory, so unless you've recreated the /home filesystem in the directory from which you're running the PHP program, the path is incorrect. Also, unless your username is Desktop, you're missing a directory between /home/ and Desktop/ - it should be your username. The pwd command will never return a . before the present working directory.

For your second question, please refer to the docs on calling Python from the command line. You can execute arbitrary code from the command line, which could be something along the lines of

python -c "from fetch_matches import Fetcher; Fetcher()"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.