shell_exec() PHP of a Python script

Asked

Viewed 743 times

2

The script

<?php 

$output = shell_exec('py core003.py');
echo $output;

?>

Rotate the script =>

import sys
print(sys.version)

The output is

3.0.1 (r301:69561, Feb 13 2009, 17:50:10) [MSC v.1500 64 bit (AMD64)]

inserir a descrição da imagem aqui

The problem is that if I run 'py core003.py' in Powershell for example, I get

3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]

inserir a descrição da imagem aqui It wasn’t pro php output to be the same as powershell?

I need to change from 3.0.1 to 3.6.0 (or whatever I choose), it is possible?

1 answer

2


You probably have different versions of Python installed, note that in Powershell you ran directly from the folder:

c: wampx64 www python

Which is probably not a global installation of Python, in fact I don’t even think it should be in the folder www (but this is another problem), so when you run via PHP by shell_exec without the path, it looks for the global installation, which is configured in the variable of the operating system, calling for PATH (environment variable), so that PHP recognizes the same as you did in powershell, you can just do so:

<?php

$pypath = 'c:\wampx64\www\python\py';
$scriptpath = 'core003.py';

$output = shell_exec(escapeshellarg($pypath) . ' ' . escapeshellarg($scriptpath));
echo $output;

Extra

You don’t need to install in the folder www, it is sufficient that the Python is in the environment variables (Environment variables), being Windows just follow the step by step:

  • Right-click the mouse/mouse on My Computer and Estates
  • In the left side menu click on Advanced system settings
  • Look for a button called Environment variables, click on it
  • Look for the variable PATH and click twice, it should return something like:

    C: Python3.0;C: Python3.0 Scripts;C: php;C: Users Latitude E6410 Appdata Roaming Composer vendor bin;C: Users Latitude E6410 Appdata Roaming npm

In my case I have Python3, Composer, php and npm, in yours must be something similar, in case you will change what is with Python (which probably points to your Python 3.0.1) to the location of your most updated Python, for example:

C: wampx64 www python;C: wampx64 www python Scripts;C: php;C: Users Latitude E6410 Appdata Roaming Composer vendor bin;C: Users Latitude E6410 Appdata Roaming npm

Click on all buttons OK until you have closed all windows that opened earlier, log on to the Windows user and Log again, ready should work your most updated Python without needing to point the way.

  • Thanks, you saved me. !

  • @Luandevecchi just followed what is in his screenshot, which indicates that it is in the www folder and there should be more than one installation.

  • Yeah, I really do. Another thing, I know the contents of the PHP page in utf-8 and still keep getting symbols instead of accents, do you think it’s something related to the script in py, or did I miss? header('Content-Type: text/html; charset=utf-8');

  • @Luandevecchi these symbols come from $output or elsewhere?

  • They come from $output yes!

  • @Luandevecchi tries so echo utf8_encode($output);

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.