1

I have a PHP script to execute a Python script.

I want to use for example numpy and other modules in my Python script.

But when I add import numpy as np or import numpy to my Python script. My output is empty and the python script stops working.

Below you find my scripts without the imports.

How can I import modules like numpy, pandas etc?

Versions:

  • PHP Version: 5.5
  • Python Version 3.4

index.php

// Execute Python Script
echo 'Output:<br />';
$command = escapeshellcmd('/var/www/html/python/test.py fileName1.txt');
$output = shell_exec($command);

// Output Python Script
echo $output;

test.py

#!/usr/local/bin/python3

# Imports
import sys
print("\n".join(sys.path))
import pip


# Check Script Arguments
if len(sys.argv) == 2:

    # Show Arguments
    print(sys.version)
    print('Python File:', str(sys.argv[0]), '<br />')
    print('Upload File:', str(sys.argv[1]), '<br />')


    installed_packages = pip.get_installed_distributions()
    installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
         for i in installed_packages])
    print(installed_packages_list)

else:

    # Missing Arguments
    print('Missing Arguments');

Result and Output:

Output:
/var/www/html/python /usr/local/lib/python34.zip /usr/local/lib/python3.4 /usr/local/lib/python3.4/plat-linux /usr/local/lib/python3.4/lib-dynload /usr/local/lib/python3.4/site-packages 3.4.3 (default, Oct 14 2015, 08:59:07) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]
Python File: /var/www/html/python/test.py
Upload File: fileName1.txt
['nltk==3.1', 'nose==1.3.7', 'numpy==1.10.1', 'oauthlib==1.0.3', 'pandas==0.17.0', 'pip==7.1.2', 'pymysql==0.6.7', 'python-dateutil==2.4.2', 'pytz==2015.6', 'requests-oauthlib==0.6.0', 'requests==2.8.1', 'scikit-learn==0.16.1', 'scipy==0.15.0', 'setuptools==18.4', 'six==1.10.0', 'sklearn==0.0', 'textblob==0.11.0', 'twython==3.3.0', 'vadersentiment==0.5']

If I add import numpy as np or import numpy to my Python script it stops working and the output is empty.

How can I import modules like numpy, pandas etc.?

If I add import numpy as np to my Python script and run the Python file with command line its working. So what can the problem be?

python3 /var/www/html/python/text.py fileName1.txt
rraayy
  • 119
  • 8
  • Some error could be raised in python. Try to add this to see the errors `import sys; sys.stderr = sys.stdout`. – Delimitry Jan 11 '16 at 13:40
  • @rraayy 1. What happens if you start interactive python from the command line (with just `python3` by itself and not `python3 /var/www/html/python/text.py fileName1.txt`) and then type `import numpy` (and press Return) in the interactive Python interpreter? 2. How are you running your script, if not from the command line? – Beetle Jan 11 '16 at 14:44
  • I fixed it! It was a memory error in numpy. http://stackoverflow.com/questions/3762566/occasional-ctypes-error-importing-numpy-from-mod-wsgi-django-app/6769624#6769624 Thanks Delimitry – rraayy Jan 12 '16 at 11:10
  • @rraayy, can you please look into this question http://stackoverflow.com/questions/35386387/run-python-script-through-php-on-browser . Badly stuck there. Seems same issue as yours. – driftking9987 Feb 14 '16 at 21:20
  • I tried commenting out the line which was suggested in the link of your comment.But it's not working for me. Output is still empty for me. – driftking9987 Feb 14 '16 at 22:12

0 Answers0