无法从PHP运行python代码-无输出

时间:2019-12-09 09:03:22

标签: php python

这是我的文件Final2.py的样子。单独运行时提供输出。我想从PHP文件访问此文件,这是我的项目的一部分。

应运行此代码的PHP文件如下:

<?php 
$command = escapeshellcmd('/Applications/XAMPP/xamppfiles/htdocs/FinalProject/Final2.py'); 
$output = shell\_exec($command);
echo $output; 
?>
#!/usr/bin/pytho
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random as rd
from collections import defaultdict
import matplotlib.cm as cm
import mysql.connector

def calculate(): 
   dataset=pd.read_csv('/Applications/XAMPP/xamppfiles/htdocs/FinalProject/Mall_Customers.csv')
   X = dataset.iloc[:, [3, 4]].values
   m=X.shape[0]
   _n=X.shape[1] 
   K=5 
   Centroids=np.array([]).reshape(2,0)
   _i=0
   for _i in range(K):
     rand=rd.randint(0,m)
     Centroids=np.c_[Centroids,X[rand]]
   num_iter=100
   for _n in range(num_iter):
    #step 2.a
     EuclidianDistance=np.array([]).reshape(m,0)
     for k in range(K):
        tempDist=np.sum((X-Centroids[:,k])**2,axis=1)
        EuclidianDistance=np.c_[EuclidianDistance,tempDist]
     C=np.argmin(EuclidianDistance,axis=1)+1

   return C

ret=calculate()
print(ret)

1 个答案:

答案 0 :(得分:0)

我认为这是因为您使用的PHP函数是miss_spelled,请尝试以下代码:

<?php 
$command = 
escapeshellcmd('/Applications/XAMPP/xamppfiles/htdocs/FinalProject/Final2.py'); 
$output = shell_exec($command);
echo $output; 
?>