1

PHP code

<?php 
echo shell_exec(' python /Users/rushikesh/Sites/hello2.py ');
>?

Python code

print('hello')

import numpy as np

print('hello2')

It outputs only hello, why does the code doesnt give any output after import statements.

I even tried finding error

echo shell_exec(' python /Users/rushikesh/Sites/hello2.py ');

But still am getting blank output after import statements

4
  • How can you tell which print('hello') is being echoed in php? Commented Feb 18, 2019 at 18:16
  • Earlier part is being echoed because i tried changed the print statements Commented Feb 18, 2019 at 18:23
  • Please check if you are executing the file which you've shown above, also try changing the string from 'hello' to anything to check which line is not executing Commented Feb 18, 2019 at 18:23
  • I have tried, earlier code is being executed before the import statement Commented Feb 18, 2019 at 18:25

2 Answers 2

1
putenv('PYTHONPATH=/Users/rushikesh/anaconda3/lib/python3.6/site-packages:');

$command = escapeshellcmd('/Users/rushikesh/anaconda3/bin/python /Users/rushikesh/Sites/hello2.py');


output = shell_exec($command);

echo $output;

Found from answer of 'call python script *with non-standard library imported* from php'

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

1 Comment

According to that question, the PYTHONPATH environment variable might not be set when using this script on a server. So setting it might be required, making this the correct answer.
0

Use python -u to skip Python's output buffer, but that still doesn't quite help if Python's library path is incorrect (exit status 3221225477). This works:

PHP start.
<?php 
$cmd = 'c:/python36-32/python -u hello.py';
echo shell_exec($cmd);
?>
PHP end.
print("Hello!")
#import sys; print(sys.path)  # Can we even find numpy?
#help('modules')  # Slow, so use -u flag for Python.
import numpy
print("Imported numpy.")

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.