【问题标题】:TensorFlow is returning an error when used in Laravel project, why?TensorFlow 在 Laravel 项目中使用时返回错误,为什么?
【发布时间】:2018-03-27 10:11:32
【问题描述】:

我从 bash 运行这个命令(在我的例子中是 zsh)

python images/classify_image.py --image_file images/new_name.jpg

我得到了正确的输出:

power drill (score = 0.97464)
hand blower, blow dryer, blow drier, hair dryer, hair drier (score = 0.00101)
carpenter's kit, tool kit (score = 0.00043)
screwdriver (score = 0.00034)
joystick (score = 0.00028)

但是当我尝试在我的 laravel 项目中重现相同的东西时,我遇到了一个错误。我的控制器内的代码是这样的:

$process = new Process('python images/classify_image.py --image_file images/new_name.jpg');

$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
   throw new ProcessFailedException($process);
}
dd($process->getOutput());

我正在使用 Composer - Process class 来执行 Laravel 内置的命令。运行此代码后,我收到此错误:

命令“python images/classify_image.py --image_file images/new_name.jpg”失败。退出代码:1(一般错误)工作目录:/var/www/html/share/public 输出:================ 错误输出:======== ======== Traceback(最近一次调用最后一次):文件“images/classify_image.py”,第 46 行,在 import tensorflow as tf ImportError: No module named tensorflow

有人可以给我提示,为什么我会收到这个错误? Laravel 似乎无法访问 TensorFlow,但是为什么我能够从不在 Laravel 项目中的 shell 执行此命令?

【问题讨论】:

  • TensorFlow 通常安装在某种封闭环境中,您的普通用户可以访问该环境 - 但 php 不会。我不知道是不是这样,但这是最有可能的问题。
  • @JoelHinz,可能就是这样,但我可以解决它吗?

标签: php python laravel shell tensorflow


【解决方案1】:

这不是 Laravel 的问题。您可以使用 exec() 运行命令吗?我猜它是一个路径问题。 Process 接受环境变量的第三个参数。

看看这是否有帮助:

$command = 'python images/classify_image.py --image_file images/new_name.jpg';
$cwd = null;
$envVars = [ 'HOME' => getEnv('HOME'), 'PATH' => getEnv('PATH') ];
$process = new Process($command, $cwd, $envVars);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
   throw new ProcessFailedException($process);
}
dd($process->getOutput());

编辑:

原因归结为 tensorflow 由于系统 PATH 变量未传递给 shell 而无法全局使用。

【讨论】:

  • 和 exec() 正在输出一个空数组 - []
  • 'PATH' => getEnv('PATH') 返回 false 因为 getEnv() 中没有这样的变量
  • 尝试将其粘贴到 'PATH' => 'here' 中,而不是使用 getEnv 来查看是否是问题所在
  • 这就是我的意思,如果 getEnv 返回一个空字符串,我认为您的 PHP conf 存在进一步的潜在问题。很高兴它的工作!
  • 在我的情况下,我只需要更改您提供的这行代码$envVars = ['HOME' => getEnv('HOME'), 'PATH' => '/home/dimitar/anaconda3/bin'];
猜你喜欢
  • 2021-09-04
  • 2020-04-10
  • 2015-09-19
  • 2019-05-26
  • 1970-01-01
  • 2016-12-08
  • 2021-06-08
  • 2021-02-17
  • 2018-12-11
相关资源
最近更新 更多