Asked  7 Months ago    Answers:  5   Viewed   27 times

I am trying to run a Python program using PHP. Here's the code

$command = '/usr/local/bin/python script.py file';
$temp = exec($command, $output);

This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privileges? I am pretty new to Linux and have no idea how to get this working.

Any help would be appreciated!

Edit 1:

Tried to use proc_open but nothing happens. I gave the full path to the script. Made the script executable but no luck. Any other things I can try on the server? (It's a CentOS 5)

 Answers

80

You need to pass the full path to the script and you also need to make sure that the script is readable by the user running the web server (which means every directory in the path must be +x to the web user).

Wednesday, March 31, 2021
 
aaronhuisinga
answered 7 Months ago
14

Add 2>&1 to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong.

Wednesday, March 31, 2021
 
Palladium
answered 7 Months ago
12

First of all, what is the version of PHP?

If other .php scripts work except for phpMyAdmin, the is pretty safe to assume something is wrong with phpMyAdmin installation. (either with the files or the Virtual Host)


Troubleshoot:

  1. Can you run another script in phpMyAdmin directory? Try with a file containing phpinfo(); If it does not work then the problem is in your apache configuration.

  2. If the above is true, then the parser is not recognising phpMyAdmin/index.php as a php file, for some reason. Try reinstalling phpMyAdmin.


Thoughts:

Remove AddType application/x-httpd-php .php. You should not need it.

In PHP 5.4 magic_quotes_gpc was removed. If you're running PHP5, remove that line.

As of PHP 4.0.3, track_vars is always turned on, so remove it also.

As of PHP 4.2.0, register_globals this directive defaults to off. In PHP5.3 register_globals is deprecated, as of PHP5.4 register_globals was removed. SO if running PHP > 4.2.0 remove that line.

Saturday, May 29, 2021
 
vuliad
answered 5 Months ago
77
for f in *.csv; do
  python playlist.py "$f" "${f%.csv}list.txt"
done

Will that do the trick? This will put foo.csv in foolist.txt and abc.csv in abclist.txt.

Or do you want them all in the same file?

Thursday, August 5, 2021
 
kat_indo
answered 3 Months ago
62

Some text-editors (including Atom and Sublime) don't like user input (raw_input()). Yes, you'd have to run the file from CLI.

You could, however, get around this problem by using other text editors like Notepad++ (see this answer to run Python in notepad++ - How to Execute a Python File in Notepad ++?), where user input works fine.

If you prefer to switch to Sublime (which also has a problem with user inputs), see this answer - Sublime Text 2 console input.

If you'd want to stick with Atom, an alternative, of course, would be to hard-code the variables you are looking for in raw_input while debugging/developing (but don't forget to switch back to raw_input after debugging).

Monday, October 11, 2021
 
Elijah W. Gagne
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :