简体   繁体   English

从 php 运行 python 程序并获得 output

[英]Running a python program from php and getting output

$command = escapeshellcmd('python3 /Users/thomasbruflot/Documents/nettsider/Curling/Auto.py');
$output = shell_exec($command);
echo $output;

I have tried the code above to get a variable called "lenke" from a python script.我已经尝试使用上面的代码从 python 脚本中获取一个名为“lenke”的变量。 What I don't understand is how I am supposed to send the variable from the python script.我不明白我应该如何从 python 脚本发送变量。 This is how it looks currently:这是它目前的样子:

import matplotlib.pyplot as plt
import numpy as np
import pyimgur
import urllib.request as urllib2

f = open("/Users/thomasbruflot/Documents/nettsider/Curling/test.csv")

data = f.read()
data = data.split('\n')

data.remove(data[len(data)-1])
listexy = [data[i].split(',') for i in range(0,len(data))]

listex =[float(i[0]) for i in listexy]
listey = [float(i[1]) for i in listexy]

fig = plt.figure()
ax = plt.subplot()
plt.plot(listex,listey)
plt.title('Ditt støt')

fig.savefig('DetFunker.png')

CLIENT_ID = "2043cba6776e29d"
PATH = "/Users/thomasbruflot/Documents/nettsider/Curling/DetFunker.png"

im = pyimgur.Imgur(CLIENT_ID)
uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")
print(uploaded_image.title)
print(uploaded_image.link) 

lenke = uploaded_image.link


req = urllib2.Request(url='http://localhost:8888/Curling/AutoPHP.php?lenke=%s' % uploaded_image.link )
f = urllib2.urlopen(req)
print(f.read())

I found out that python3 was looking for the wrong path in sys.我发现 python3 在 sys.path 中寻找错误的路径。 What made it work for me was overwriting it all by adding at the top of my script:使它对我有用的是通过在我的脚本顶部添加来覆盖它:

''' import sys sys.path = correct path ''' Very funky solution as I don't think one should have to overwrite the path (time-consuming and unnecessary). ''' import sys sys.path = correct path ''' 非常时髦的解决方案,因为我认为不必覆盖路径(耗时且不必要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM