How to run .php in python?

3

Good day, I have the following script in python that is called test.py :

import subprocess

subprocess.call(["php", "load_data.php"])

The idea is to run another script php in which I do LOAD DATA INFILE to a table of MySQL , but it generates the following error:

Traceback <most recent call last>:
    File "test.py", line 3, in <module>
    subprocess.call<["php","load_data.php"]>
File "C:\Python34\lib\subprocess.py", line 537, in call
   with Popen<*popenargs, **kwargs> as p:
File "C:\Python34\lib\subprocess.py" line 859, in __init__
   restore_signals, start_new_session>
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo>
FileNotFoundError: [WinError 2] The system cannot find the file specified 

If I run the script load_data.php direct in a web browser, it works without any problem so I think the problem is in python , it should be mentioned that I have done this correctly before but with python 3.6 , some idea if I need to configure something or add a library.

I have php5.3 , apache 2.2.21 , mysql5.5.20 , python3.4

Greetings!

    
asked by El Cóndor 18.09.2018 в 01:11
source

1 answer

0

First of all, I created a folder with the programs to run.

workspace/
    |-- main.py
    |-- main.php

The content of main.py is:

import subprocess

def main():
    subprocess.call(["php", "main.php"])

if __name__ == '__main__':
    main()

And the one in main.php is:

<?php
echo "Dentro de archivo php";
?>

It worked correctly.

I tried to reproduce the error in two different ways. The first one was missing the file name main.php and I got the error:

  

Could not open input file:

The second one was missing the name of the program that opens it, i.e, "php" and I got the error you describe.

So, as I understand it, maybe you have to check that you can run your php script from the terminal with a simple:

$php tu_script.php

If you can not run it, but you're sure you have php installed, you can configure the environment variables to add your php script to a variable (for Windows) or add the path to your path or make a symbolic link from where it is find your "php" file to / usr / local / bin / php (for Linux distributions) and then make sure you can run your script from your console.

If you are in windows, remember that every time you edit an environment variable (PATH, in this case) and accept, etc, etc., to not open and close terminals, execute the command refreshenv and recharge the environment variables.

This I commented to you I successfully tested it in Windows 10 with both python 2.7.13 and with python 3.4.0 and python 3.6.1

    
answered by 06.11.2018 в 15:35