我正在使用symfony / process在Laravel中调用Python脚本。我得到了:
The command "python /home/nlkduy/compare.py" failed. Exit Code: 1(General error) Working directory: /var/www/html/diembao/public Output: ================ Error Output: ================ Traceback (most recent call last): File "/home/nlkduy/compare.py", line 1, in <module> import mysql.connector ImportError: No module named mysql.connector
这是我在Laravel Controller中的代码:
public function compare()
{
$process = new Process('python /home/nlkduy/compare.py');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
}
这是Python脚本:
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "***",
passwd = "***",
database = "***"
)
mycursor = mydb.cursor()
sql = "INSERT INTO role(id, name) VALUES (%s, %s)"
val = ("5e4t", "ABC")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
我确定安装了mysql.connector,因为我在终端上收到了以下消息:
nlkduy@acer:~$ python compare.py
(1, 'record inserted.')