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