1

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..")
2
  • What privileges does the python script have? Php runs exec as web user not as root. Commented Jul 1, 2017 at 23:22
  • @Björn, good call! ls -al shows me -rw-r--r-- 1 Naltroc 197121 150 Jul 1 18:55 printer.py. So that means owner: read write, group: read, others: read if memory serves. In general, Is it safe to set others to execute? The script can only be called through the controller or command interface, so it seems safe . . . Commented Jul 1, 2017 at 23:25

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.