Naltroc
Naltroc

Reputation: 1007

Running python script in Laravel, script runs but does not execute entirely?

Running on a local Apache setup on my machine, and I use python outside of Laravel.

Using Laravel 5.4, I have made and registered the command. I can call it fine using php artisan python:printIt.

I am getting a success on running the command. . . but the file is not writing! Here is the console message:

$ php artisan python:printIt In command handle for printIt. Running command. Command is : C:\Users\Cortland\Miniconda3\python.exe C:^\wamp64^\www^\cmrd^\cgi-bin^\py-test^\printer.pySuccess. Here is the return value: This message came from the python script.1

The python script is fine when run through the python shell or command line. Here, must be running the script because it has the correct print message. However, the exec return value is a 1, which is failure.

The failure is confirmed in that no file 'pyfyle.txt' exists.

What am I doing incorrectly, and how can it be fixed? The real application will be more involved, but have to make sure I can operate basic script calls first.

The Laravel command class:

printIt.php

class printIt extends Command
{
    protected $signature = 'python:printIt';

    protected $description = 'Run the test python command.';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        echo "In command handle for printIt.\n";
        exec("where python", $path);

        $cmd = base_path('cgi-bin\py-test\printer.py');

        // "path/to/python command"
        $run = $path[0] . ' ' . escapeshellcmd($cmd);

        try {
            echo "Running command.\n";
            echo "Command is : " . $run;
            $res = exec($run, $out, $res);

            if ($res == 1) {
                throw \Exception("Could not run the sript");
            } else {
                echo "Success. Here is the return value: ", print_r($res);
            }
        } catch (\Exception $e) {
            echo "Error running command. It says " . $e->getMessage();
        }
    }
}

The python script:

printer.py

with open('pyfyle.txt', 'w', encoding='utf-8') as f:
    f.write('new text here.')
    f.close

// Edit: error, incorrect call to close: change f.close to f.close()

print("This message came from the python script..")

Upvotes: 1

Views: 1628

Answers (2)

Naltroc
Naltroc

Reputation: 1007

The issue was I had made an assumption about the version of python the server was running.

At home I use Python 3.6. If I had simply ran python -V on the server at the beginning, I would have prevented hours of confundrum and frustration.

The server runs on Python 2.6, where file operation is not the same. kwarg encoding is not valid, which is enough to break the program.

Also, I should have been printing $res to see the error messages.

Moral of the story:

Always check software and package versions.

Always find a way to show your errors.

Upvotes: 0

Cyril Graze
Cyril Graze

Reputation: 3890

In console enter:

sudo chmod +x printer.py

To give the Python script execution permissions.

That would work for Linux distributions, but seeing that you have "C:\" in your output, it means you're running Windows.

In which case:

you need to add .py to the PATHEXT environment variable.

as per: https://docs.python.org/3/faq/windows.html#how-do-i-make-python-scripts-executable

Upvotes: 1

Related Questions